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.
Discussions

Best way to check for null on Strings?
You can use StringUtils.hasText(string) . Will return false if its null More on reddit.com
🌐 r/learnjava
16
6
May 8, 2024
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 ...
Using "abc".equals(...) is a reasonable precaution against a condition you didn't anticipate, but doesn't really solve any problems. Maybe this particular method doesn't care that myString is null (the string comparison returns false, but that's it), but what about elsewhere? More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
March 24, 2016
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.string.equals
String.Equals Method (System) | Microsoft Learn
The second string to compare, or null. ... true if the value of a is the same as the value of b; otherwise, false. If both a and b are null, the method returns true. The following example demonstrates the Equals method.
🌐
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....
🌐
Reddit
reddit.com › r/learnjava › best way to check for null on strings?
Best way to check for null on Strings? : r/learnjava
May 8, 2024 - The real solution here is to not allow null. Use ImmutableMap. ... You seem to try to compare String values with == or !=. This approach does not work reliably in Java as it does not actually compare the contents of the Strings. Since String is an object data type it should only be compared using .equals().
🌐
Baeldung
baeldung.com › home › java › java string › comparing strings in java
Comparing Strings in Java | Baeldung
June 19, 2024 - Two null values are considered equal. Furthermore, this method can be used to sort a list of Strings with null entries:
🌐
W3Docs
w3docs.com › java
How to check if my string is equal to null?
To check if a string is equal to null in Java, you can use the == operator.
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › equals
Java String equals() - Compare Strings Equality
December 23, 2024 - By incorporating these best practices and using equals() correctly, empower your Java applications with reliable, error-free string comparison mechanisms, enhancing both security and functionality. Ensure to always consider potential null values and remember the case sensitivity in comparisons ...
🌐
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 - For this type of thing I switched it around and did it like "MyValue".equals(myVar), which won't throw an exception if myVar is null. I know that doesn't completely address your particular issue. For your problem, you might want to consider writing a function to handle your comparison, particularly if you do this often in your code. You could go with the try/catch or a couple of if/then statements to check for null before doing the comparison inside the function. ... Assuming your String reference variables are strObj1 and strObj2, you could do the following: -Peter
🌐
How to do in Java
howtodoinjava.com › home › string › java string.equals()
Java String.equals() with Examples - HowToDoInJava
January 9, 2023 - The String.equals() in Java compares a string with the object passed as the method argument. It returns true if and only if: the argument object is of type String · the argument object is not null · represents the same sequence of characters ...
🌐
JetBrains
jetbrains.com › help › inspectopedia › StringEqualsEmptyString.html
'String.equals()' can be replaced with 'String.isEmpty()' | Inspectopedia Documentation
December 3, 2025 - For safety, this inspection's quick-fix inserts an explicit null-check when the equals() argument is nullable. Use the option to make the inspection ignore such cases. Inspection ID: StringEqualsEmptyString · Here you can find the description of settings available for the 'String.equals()' can be replaced with 'String.isEmpty()' inspection, and the reference of their default values.
🌐
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 - Both strings are null. 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.
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › comparing-strings-with-possible-null-values-in-java
Comparing Strings with (possible) null values in java?
import java.util.Scanner; public class CompringStrings { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter your first string value: "); String str1 = sc.next(); System.out.println("Enter your second string value: "); String str2 = sc.next(); //Comparing two Strings int res = str1.compareTo(str2); System.out.println(res); if(res==0) { System.out.println("Both Strings are null or equal"); }else if(res<0){ System.out.println(""+str1+" preceeds "+str2+""); }else if(res>0){ System.out.println(""+str2+" preceeds "+str1+""); } } }
🌐
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.
🌐
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 - 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. var1 == null ? false : var1.equals(var2) remember to think about how you want to handle the edge case where both are null as well (if that is indeed a valid scenario).
🌐
YouTube
youtube.com › watch
How to avoid the NullPointerException when using .equals - YouTube
The .equals method is often used within if-statements and to return boolean expressions within an application.By understanding how the structure of the boole...
Published   August 30, 2021