string == null compares if the object is null. string.equals("foo") compares the value inside of that object. string == "foo" doesn't always work, because you're trying to see if the objects are the same, not the values they represent.


Longer answer:

If you try this, it won't work, as you've found:

String foo = null;
if (foo.equals(null)) {
    // That fails every time. 
}

The reason is that foo is null, so it doesn't know what .equals is; there's no object there for .equals to be called from.

What you probably wanted was:

String foo = null;
if (foo == null) {
    // That will work.
}

The typical way to guard yourself against a null when dealing with Strings is:

String foo = null;
String bar = "Some string";
...
if (foo != null && foo.equals(bar)) {
    // Do something here.
}

That way, if foo was null, it doesn't evaluate the second half of the conditional, and things are all right.

The easy way, if you're using a String literal (instead of a variable), is:

String foo = null;
...
if ("some String".equals(foo)) {
    // Do something here.
}

If you want to work around that, Apache Commons has a class - StringUtils - that provides null-safe String operations.

if (StringUtils.equals(foo, bar)) {
    // Do something here.
}

Another response was joking, and said you should do this:

boolean isNull = false;
try {
    stringname.equalsIgnoreCase(null);
} catch (NullPointerException npe) {
    isNull = true;
}

Please don't do that. You should only throw exceptions for errors that are exceptional; if you're expecting a null, you should check for it ahead of time, and not let it throw the exception.

In my head, there are two reasons for this. First, exceptions are slow; checking against null is fast, but when the JVM throws an exception, it takes a lot of time. Second, the code is much easier to read and maintain if you just check for the null pointer ahead of time.

Answer from Dean J on Stack Overflow
Top answer
1 of 16
187

string == null compares if the object is null. string.equals("foo") compares the value inside of that object. string == "foo" doesn't always work, because you're trying to see if the objects are the same, not the values they represent.


Longer answer:

If you try this, it won't work, as you've found:

String foo = null;
if (foo.equals(null)) {
    // That fails every time. 
}

The reason is that foo is null, so it doesn't know what .equals is; there's no object there for .equals to be called from.

What you probably wanted was:

String foo = null;
if (foo == null) {
    // That will work.
}

The typical way to guard yourself against a null when dealing with Strings is:

String foo = null;
String bar = "Some string";
...
if (foo != null && foo.equals(bar)) {
    // Do something here.
}

That way, if foo was null, it doesn't evaluate the second half of the conditional, and things are all right.

The easy way, if you're using a String literal (instead of a variable), is:

String foo = null;
...
if ("some String".equals(foo)) {
    // Do something here.
}

If you want to work around that, Apache Commons has a class - StringUtils - that provides null-safe String operations.

if (StringUtils.equals(foo, bar)) {
    // Do something here.
}

Another response was joking, and said you should do this:

boolean isNull = false;
try {
    stringname.equalsIgnoreCase(null);
} catch (NullPointerException npe) {
    isNull = true;
}

Please don't do that. You should only throw exceptions for errors that are exceptional; if you're expecting a null, you should check for it ahead of time, and not let it throw the exception.

In my head, there are two reasons for this. First, exceptions are slow; checking against null is fast, but when the JVM throws an exception, it takes a lot of time. Second, the code is much easier to read and maintain if you just check for the null pointer ahead of time.

2 of 16
33
s == null

won't work?

🌐
Coderanch
coderanch.com › t › 545457 › java › comparing-values-equalsIgnoreCase-equals-NULL
comparing two values with equalsIgnoreCase and equals with NULL as first value. (Java in General forum at Coderanch)
java.lang.NullPointerException at EqualIgnoreForNull.main(EqualIgnoreForNull.java:11) Exception in thread "main" HERE the second element in the equalsIgnoreCase and equals is null and the first element has value,but it does not throw NullPointerException Why it does behave like this?
🌐
Scaler
scaler.com › home › topics › equalsignorecase() in java
equalsIgnoreCase() in Java - Scaler Topics
April 26, 2022 - The equalsIgnoreCase() method doesn’t throw an exception, if the input string is null, it will return false.
🌐
CodeGym
codegym.cc › java blog › strings in java › string equalsignorecase() method in java
String equalsIgnoreCase() Method in Java
February 13, 2025 - It does not handle null values, meaning that calling this method on a null string will result in a NullPointerException. It also does not ignore whitespace, so "hello " and "hello" will still be considered different.
🌐
Fantom
fantom.org › forum › topic › 2347
Should Str.equalsIgnoreCase(Str str) accept null? – Fantom
equalsIgnoreCase is not really like equals. It is a Str only comparison with a slightly misleading name. I agree with @tomcl, its not really apples to apples. By convention you should really be using == to test equality and it has special handling for nullable on either side.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-equalsignorecase-method-with-examples
Java String equalsIgnoreCase() Method - GeeksforGeeks
December 23, 2024 - In Java, equalsIgnoreCase() method ... upper) of the string. This method returns a boolean value, true if the argument is not null and represents an equivalent String ignoring case, else false....
🌐
How to do in Java
howtodoinjava.com › home › string › java string equalsignorecase()
Java String equalsIgnoreCase() with Examples - HowToDoInJava
January 6, 2023 - boolean isEqual = thisString.equalsIgnoreCase( anotherString ); Note that if we pass null as the method argument, the comparison result will be false.
🌐
Baeldung
baeldung.com › home › java › java string › java string equalsignorecase()
Java String equalsIgnoreCase() | Baeldung
January 8, 2024 - String lower = "equals ignore case"; String UPPER = "EQUALS IGNORE CASE"; assertThat(lower.equalsIgnoreCase(UPPER)).isTrue(); The Apache Commons Lang library contains a class called StringUtils that provides a method similar to the method above, but it has the added benefit of handling null values:
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › equalsIgnoreCase
Java String equalsIgnoreCase() - Compare Ignoring Case | Vultr Docs
December 18, 2024 - Avoid this error by adding null checks before invoking comparison methods on strings possibly holding null. The equalsIgnoreCase() function in Java is an essential tool for string comparison that disregards the case of characters.
🌐
Java2s
java2s.com › example › android › java.lang › string-equals-ignore-case-and-handle-null-value.html
String equals Ignore Case and handle null value - Android java.lang
String equals Ignore Case and handle null value · import java.net.MalformedURLException; import java.net.URL; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.StringTokenizer; import java.util.regex.Matcher; import ...
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java NullPointerException Avoidance and Enhancement Tactics - Java Code Geeks
March 1, 2021 - The code listing and associated output that follow demonstrate null-safe use of String.equalsIgnoreCase(String). These last two demonstrations used literal strings as the “known non-null” strings against which methods were called, but other strings and objects could also be used.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_equalsignorecase.htm
Java - String equalsIgnoreCase() Method
public boolean equalsIgnoreCase(String anotherString) anotherString − This is the String to compare this String against. This method returns true if the argument is not null and it represents an equivalent String ignoring case, else false.
🌐
W3Schools
w3schools.com › java › ref_string_equalsignorecase.asp
Java String equalsIgnoreCase() Method
The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences.
🌐
Katalon
forum.katalon.com › product forums › katalon studio
java.lang.NullPointerException: Cannot invoke method equalsIgnoreCase() on null object - Katalon Studio - Katalon Community
April 4, 2022 - Hi, I am trying to read the csv file and fetch the headers and related values in the map and convert it to a list because I want two different lists of emails matching with specific column values as Y and N. If that column is Y, it should return some list of emails present in the CSV file, and if it is N, it should return another list of emails.
🌐
Javatpoint
javatpoint.com › java-string-equalsignorecase
Java String equalsIgnoreCase() method - javatpoint
Java String equalsIgnoreCase() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string equalsignorecase in java etc.
🌐
David Merrick
david-merrick.com › 2014 › 10 › 09 › use-object-equals-instead-of-string-equalsignorecase-to-avoid-a-nullpointerexception-in-java
Use Object.equals() instead of String.equalsIgnoreCase() to avoid a NullPointerException in Java \| David Merrick
October 9, 2014 - 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 ...
🌐
Liquibase
forum.liquibase.org › general discussion
Cannot invoke "String.equalsIgnoreCase(String)" because "this.tableName" is null - General Discussion - Liquibase
August 9, 2022 - Hi, While running liquibase for cassandra, --liquibase formatted sql --changeset user1:25 CREATE KEYSPACE IF NOT EXISTS key1 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1' } ; USE key1; CREATE TABLE IF NOT EXISTS xxx ( .... .... .... .... ); I am getting this error Caused by: liquibase.exception.LiquibaseException: java.lang.NullPointerException: Cannot invoke "String.equalsIgnoreCase(String)" because "this.tableName" is null at liquibase.Liquibase.runInSco...