15
Oct.
2003
Tips 'n' Tricks: Prevent NullPointerException in string comparison
Very often I read the following statement:
if (someString.equals("foo")) ...
Now if someString is null this code would produce a nasty NullPointerException. One solution might be:
if ((someString != null) && someString.equals("foo")) ...
But there's a better way:
if ("foo".equals(someString)) ...
Very easy =)
Comments