CodePudding user response:
You need to explicitly check against the literal value null
. In the following example, if the non-null check fails, it wont' run the statement after the &&
; the first check short-circuits the rest of the expression
Object obj;
if (obj != null && obj.toString().equals("string")) {
}
else {
}
Note:
null
is a literal value (similar to true and false). So you should not be comparing it like a String
.