I use this approach:
if (null == drawable) {
//do stuff
} else {
//other things
}
This way I find improves the readability of the line - as I read quickly through a source file I can see it's a null check.
With regards to why you can't call .equals()
on an object which may be null
; if the object reference you have (namely 'drawable') is in fact null
, it doesn't point to an object on the heap. This means there's no object on the heap on which the call to equals()
can succeed.
Best of luck!