I want to initialize a String variable and later assign a value to it using
if (){} else if(){}
This is what I have done:
String roleName;
// A and B are string constants
if (userRoleslist.contains(A)) {
roleName = A;
} else if (userRoleslist.contains(B)) {
roleName = B;
}
if (!roleName.equals(null)) {
audit.info("User: " + userName + " successfully authorized as " +
roleName + " to perform JMX operations.");
return roleName;
} else {
String msg = "User: " + userName + " not authorized to perform JMX operations.";
log.error(msg);
throw new NullPointerException();
}
The problem is that the error is not logged to the console. Only the NullPointerException is thrown and point the line if
(!roleName.equals(null)) {
Also, additionally can I leave out the NullPointerException and a log.error only?
audit
, error goes tolog
in your code. Sure you have two different loggers?roleName != null
. The "null first in conditions" is pretty outdated, pointless in Java and widely regarded as less readable.roleName.equals(null)
can never return true. Well... not if implemented correctly.