Use

stringVariable == null

To test whether stringVariable is null.

The equals method (and every other method) requires stringVariable to not be null.

Answer from Pool 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. The better way to do it will be like follows. if (theStringIknow.equals(someString)) { System.out.println("Same same"); }
🌐
EqualsVerifier
jqno.nl › equalsverifier › errormessages › non-nullity-equals-hashcode-tostring-throws-nullpointerexception
Non-nullity: equals/hashCode/toString throws NullPointerException - EqualsVerifier
3 weeks ago - This error occurs when the class under test can throw a NullPointerException when one of its fields is null and equals/hashCode/toString is called. For example, equals could contain this line: return foo.equals(other.foo); It will throw a NullPointerException if foo is null.
🌐
GeeksforGeeks
geeksforgeeks.org › java › null-pointer-exception-in-java
Null Pointer Exception in Java - GeeksforGeeks
August 5, 2025 - A NullPointerException in Java is a RuntimeException. It occurs when a program attempts to use an object reference that has the null value.
🌐
Blogger
javarevisited.blogspot.com › 2013 › 05 › ava-tips-and-best-practices-to-avoid-nullpointerexception-program-application.html
Java Tips and Best practices to avoid NullPointerException in Java Applications
By the way ,this is relatively new best practice for Java programmers and it will take some time to get adopted. Despite of other disadvantages like creating temporary object, autoboxing are also prone to NullPointerException, if the wrapper class object is null. For example, following code will fail with NullPointerException if person doesn't have phone number and instead return null. ... Not just equality but < , > can also throw NullPointerException if used along autoboxing and unboxing.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 391727 › java › String-equals-throws-null-exception
String.equals() throws null exception (Beginning Java forum at Coderanch)
A NullPointerException is "thrown when an application attempts to use null in a case where an object is required" not when comparing two strings that are different. In your example, plaat must not have been properly initialized, as it is null. [ June 04, 2002: Message edited by: Dirk Schreckmann ...
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › java basics › exceptions
java.lang.NullPointerException - Examples Java Code Geeks - 2025
July 6, 2022 - In Java, a special null value can ... of data. A java.lang.NullPointerException is thrown when an application is trying to use or access an object whose reference equals to null....
🌐
Errorprone
errorprone.info › bugpattern › EqualsNull
EqualsNull
either returns false, or throws a NullPointerException if x is null. The nested block may never execute. This check replaces x.equals(null) with x == null, and !x.equals(null) with x != null. If the author intended for x.equals(null) to return true, consider this as fragile code as it breaks ...
🌐
Blogger
tjisblogging.blogspot.com › 2020 › 06 › java-string-nullpointerexception-safe.html
Java String NullPointerException safe equals check
In this case, the unknown string is null, which means it doesn't exist. Thus a NullPointerException. The better way to do it will be like follows. if (theStringIknow.equals(someString)) { System.out.println("Same same"); } In that case, you are always invoking the equals method of a String that you are pretty sure it exists.
🌐
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
🌐
Rollbar
rollbar.com › home › how to catch and fix nullpointerexception in java
NullPointerException Crash Your Java App? Here's How to Fix It
November 29, 2025 - In Java, primitives cannot have null values. Writing methods that return empty objects rather than null where possible. For example, returning empty collections and empty strings from a method. To fix the NullPointerException in the earlier example, the String object should be checked for null or empty values before it is used any further:
🌐
How to do in Java
howtodoinjava.com › home › exception handling › java nullpointerexception
Handling Java NullPointerException and Best Practices
October 1, 2022 - Java NullPointerException is an unchecked exception and extends RuntimeException. Learn why NullPointerException occur and how to handle it in the code.
🌐
Blogger
marxsoftware.blogspot.com › 2021 › 02 › java-nullpointerexception-avoidance-and.html
Inspired by Actual Events: Java NullPointerException Avoidance and Enhancement Tactics
The advantage of this approach is that the operation used to compute that default value will only be executed if the object is null and the cost of executing that Supplier is NOT incurred if the specified object is null (Supplier deferred execution). ... Although Java enums can be compared for equality using Enum.equals(Object), I prefer to use the operators == and != for comparing enums because the latter is null-safe (and arguably makes for easier reading).
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › NullPointerException.html
NullPointerException (Java Platform SE 8 )
October 20, 2025 - Applications should throw instances ... of the null object. NullPointerException objects may be constructed by the virtual machine as if suppression were disabled and/or the stack trace was not writable. ... addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString · clone, equals, finalize, ...
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › NullPointerException.html
NullPointerException (Java SE 17 & JDK 17)
October 20, 2025 - If no explicit message was passed to the constructor, and as long as certain internal information is available, a verbose description of the null reference is returned. The internal information is not available in deserialized NullPointerExceptions.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › NullPointerException.html
NullPointerException (Java Platform SE 7 )
Java™ Platform Standard Ed. 7 ... Thrown when an application attempts to use null in a case where an object is required.