You cannot use the dereference (dot, '.') operator to access instance variables or call methods on an instance if that instance is null. Doing so will yield a NullPointerException.

It is common practice to use something you know to be non-null for string comparison. For example, "something".equals(stringThatMayBeNull).

Answer from sjr on Stack Overflow
🌐
Medium
medium.com › @thilinajayawardana_85346 › java-string-nullpointerexception-safe-equals-check-404481934e9b
Java String NullPointerException safe equals check | by Thilina Jayawardana | Medium
June 30, 2020 - This is because, in the if condition, you are first using the unknown string’s equals method to compare the other string. In this case, the unknown string is null, which means it doesn’t exist. Thus a NullPointerException.
Discussions

Java comparison == is not null-safe?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
41
16
February 21, 2024
Best Way to Check if a String is Null or Empty in Java - TestMu AI Community
What is the best way to check if a String is null or empty in Java? I’m currently parsing HTML data, and sometimes the String can be null or empty when the expected word doesn’t match. To handle this, I wrote the follow… More on community.testmuai.com
🌐 community.testmuai.com
0
March 19, 2025
java - Is "use "abc".equals(myString) instead of ...
11 When comparing a string variable to a string literal with .equals(), is there a standard practice for the order of items? -2 Question about the Java objects' equals() method · 128 Why use Optional in Java 8+ instead of traditional null pointer checks? More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
March 24, 2016
java - Compare Strings avoiding NullPointerException - Stack Overflow
@saumilsdk It is not possible to call null.equals(string). In case both Strings are a variable you need to call != null on the first one in order to avoid the exception. More on stackoverflow.com
🌐 stackoverflow.com
🌐
TechVidvan
techvidvan.com › tutorials › java-string-equals-method
Java String equals() Method - TechVidvan
March 18, 2024 - String Literal : It can be applied ... unique comparison logic. Null Safety: If you try to use equals() on a null reference, it will return false rather than throwing an exception....
🌐
Baeldung
baeldung.com › home › java › java string › comparing strings in java
Comparing Strings in Java | Baeldung
June 19, 2024 - The method returns true if two Strings are equal by first comparing them using their address i.e “==”. Consequently, if both arguments are null, it returns true and if exactly one argument is null, it returns false.
🌐
TestMu AI Community
community.testmuai.com › ask a question
Best Way to Check if a String is Null or Empty in Java - TestMu AI Community
March 19, 2025 - What is the best way to check if a String is null or empty in Java? I’m currently parsing HTML data, and sometimes the String can be null or empty when the expected word doesn’t match. To handle this, I wrote the following check: if(string.equals(null) || string.equals("")){ Log.d("iftrue", "seem to be true"); }else{ Log.d("iffalse", "seem to be false"); } However, when I remove string.equals(""), the condition doesn’t work correctly.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › equals
Java String equals() - Compare Strings Equality
December 23, 2024 - Consequently, the equals() method returns false. Understand that invoking equals() on a null reference will throw a NullPointerException. Perform a null check before using equals() for comparison.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 387952 › java › avoid-null-pitfalls-comparing-Strings
How do I avoid null pitfalls when comparing Strings? (Beginning Java forum at Coderanch)
February 7, 2001 - If any is then don't do the comparison. If none of them is null, use the eqauls() method to do your comparison. Good luck, Bosun · Bosun (SCJP, SCWCD). So much trouble in the world -- Bob Marley ... The way to compare String object contents is to use the equals() method.
🌐
Qlik Community
community.qlik.com › t5 › Talend-Studio › Handling-nulll-while-doing-equals-function › td-p › 2326825
Handling nulll while doing equals function - Qlik Community - 2326825
February 4, 2017 - As we know equals function (var1.equals(var2)) does not handle null values and it thows "null pointer exception ".In other words , both variables should have values only then it can work .If I used two equals function (==) function It is not giving expected result as we know the reason. I am looking for that solution where I can compare null String with a value var1="2016-01-03 11:05:07" var2=null ... Ditto - same here! ... use the java ternary operator.
🌐
CodeGym
codegym.cc › java blog › strings in java › java: check if string is null, empty or blank
Java: Check if String is Null, Empty or Blank
October 11, 2023 - The String = null The String = Lubaina Khan · “An empty String in Java means a String with length equal to zero.” If a String is empty that means the reference variable is referring to a memory location holding a String of length equal to zero.
🌐
Quora
quora.com › How-do-perform-a-Null-check-for-string-in-Java
How do perform a Null check for string in Java? - Quora
Answer (1 of 7): 4 Ways to check if String is null or empty in Java:- Here are my four solutions for this common problem. Each solution has their pros and cons and a special use case e.g. first solution can only be used from JDK7 on wards, second solution is the fastest way to check string is em...
🌐
How to do in Java
howtodoinjava.com › home › string › java string.equals()
Java String.equals() with Examples - HowToDoInJava
January 9, 2023 - The equals() does not support null argument and throws NullPointerException. String str1 = "alex"; Assertions.assertThrows(NullPointerException.class, () -> { str1.contains(null); }); The following Java program demonstrates that equals() method ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.string.equals
String.Equals Method (System) | Microsoft Learn
public: override bool Equals(System::Object ^ obj); ... The string to compare to this instance. ... true if obj is a String and its value is the same as this instance; otherwise, false. If obj is null, the method returns false.
🌐
GeeksforGeeks
geeksforgeeks.org › java › program-to-check-if-the-string-is-null-in-java
Program to check if the String is Null in Java - GeeksforGeeks
July 12, 2025 - To check if a string is null in Java, we can use the "==" operator that directly compares the string reference with null.