Use Object.equals() instead of String.equalsIgnoreCase() to avoid a NullPointerException in Java

A NullPointerException in Java is thrown when an object is expected as a parameter, but null is passed instead. To avoid this when doing String comparisons, use Object.equals instead.

The method signatures for equals() and equalsIgnoreCase() are different (the former accepts an Object as a parameter and the latter accepts a String ), but they both return a boolean primitive value.

public boolean equals(Object anObject);
public boolean equalsIgnoreCase(String anotherString);

This code will throw a NullPointerException if stringTwo is set to null:

String stringOne = "Foo"
String stringTwo = "Bar";
boolean areTheyEqual = stringOne.equalsIgnoreCase(stringTwo);

This code serves the same function and doesn’t throw the exception.

String stringOne = "Foo"
String stringTwo = "Bar";
boolean areTheyEqual = stringOne.equals(stringTwo);

Updated:

Comments