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!