0

I have an enum declared as follows -

public enum Status {
    REQ ("URL1"),
    NOT ("URL2"),
    GET ("URL3");

    String getURL;

    Status(String getURL) {
        this.getURL = getURL;

    }
}

And a field in my class:

private Status status;

I have a function in order to retrieve the URL based on the enum type as follows -

public String viewURL() {
    switch (status) {
        case REQ:
            return REQ.getURL;
        case NOT:
            return NOT.getURL;
        case GET:
            return GET.getURL;
    }
    return null;
}

I'm encountering a NullPointerException in this method when status is null.

However when I implement the same functionality using if-statements it works fine -

public String viewURL() {
    if (status == REQ) {
        return REQ.getURL;
    }
    if (status == NOT) {
        return NOT.getURL;
    }
    if (status == GET) {
        return GET.getURL;
    }
    return null;
}

Not able to understand where I'm going wrong. Any help would be really appreciated!

Any help on re-factoring also is appreciated!

Bohemian
  • 412,405
  • 93
  • 575
  • 722
jjl
  • 15
  • 2
  • The evidence suggests that `switch (status)` is giving an NPE because `status` is `null`. You switch on `null`. – Stephen C Mar 04 '23 at 03:06
  • The switch statement is just a long-winded way of writing `return status.getURL` anyway. Or, with the bug fixed, `return status != null ? status.getURL : null`. – undefined symbol Mar 04 '23 at 03:24
  • However, when I use the above mentioned if statement I'm not encountering a null pointer exception. It is just with switch case where the status is not able to set a value. Why is it? – jjl Mar 04 '23 at 03:49
  • The switch case is 100% code noise. It does nothing more than `return status.getUrl();`. Try this: `return Optional.ofNullable(status).map(Status::getUrl).orElse(null);` – Bohemian Mar 04 '23 at 04:21
  • @Bohemian -Thank you for sharing! I'm trying to use this. However, I'm getting an error at getUrl part says "Cannot resolve method 'getLink' " – jjl Mar 04 '23 at 04:46
  • @jjl sorry; I thought it was a method. Try this: `return status.getUrl();. Try this: return Optional.ofNullable(status).map(s -> s.getURL).orElse(null);` – Bohemian Mar 04 '23 at 10:12
  • @Bohemian The later solution worked like a gem! ->``` return Optional.ofNullable(status).map(s -> s.getURL).orElse(null);``` Can you please explain it if you don't mind? Thank you very much! – jjl Mar 04 '23 at 18:43
  • @Bohemian Also, trying out just ```return status.getUrl();``` complains that method call is required. – jjl Mar 04 '23 at 18:58

2 Answers2

1

This is an ideal use case for Optional:

public String viewURL() {
    return Optional.ofNullable(status)
      .map(s -> s.getUrl) // only executes if previous step returns non-null
      .orElse(null);      // executes if any step returns null
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

If the viewURL method has access to the status variable, you can use this code:

public String viewURL() {
    if (status != null)
        return status.getURL;
    return null;
}

I don't think you have to use a switch statement because we end up using the same code in every case. That is, we return status.getURL in every case.

The only requirement here is that every instance of the Status enum has a URL.

ktm5124
  • 11,861
  • 21
  • 74
  • 119