I use object != null
a lot to avoid NullPointerException.
Is there a good alternative to this?
For example I often use:
Only for this situation -
Not checking if a variable is null before invoking an equals method (a string compare example below):
if ( foo.equals("bar") ) {
// ...
}
will result in a NullPointerException
if foo
doesn't exist.
You can avoid that if you compare your String
s like this:
if ( "bar".equals(foo) ) {
// ...
}