Correct way to check for null or empty or string containing only spaces is like this:

if(str != null && !str.trim().isEmpty()) { /* do your stuffs here */ }
Answer from Pradeep Simha on Stack Overflow
🌐
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 - The below example demonstrates how to check if a given string is null using the == relational operator. ... // Java Program to check if // a String is Null class StringNull { // Method to check if the String is Null public static boolean ...
🌐
Reddit
reddit.com › r/java › cleanest way to check for null on a string?
Cleanest way to check for null on a String? : r/java
May 8, 2024 - But as an FYI instead of "String.valueOf" you can use "java.util.Objects.toString(Object o, String nullDefault)" instead, which was added in Java 1.7. If you replaced String.valueOf(someObject.get("someKey")) with Objects.toString(someObject.get("someKey"), null) then it works just fine when ...
🌐
Programiz
programiz.com › java-programming › examples › string-empty-null
Java Program to Check if a String is Empty or Null
The method removes all the white ... if string is null or empty public static String isNullEmpty(String str) { // check if string is null if (str == null) { return "NULL"; } // check if string is empty else if (str.trim().isEmpty()){ return "EMPTY"; } else { return "neither NULL ...
🌐
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 - Very often in programming, a String is assigned null to represent that it is completely free and will be used for a specific purpose in the program. If you perform any operation or call a method on a null String, it throws the java.lang.NullPointerException. Here is a basic example illustrating declaration of a null String. It further shows how to check if it is a valid null String.
🌐
Baeldung
baeldung.com › home › java › java string › checking for empty or blank strings in java
Checking for Empty or Blank Strings in Java | Baeldung
January 8, 2024 - The Android flavor targets Android ... Strings class comes with the method Strings.isNullOrEmpty: ... It checks whether a given string is null or empty, but it won’t check for whitespace-only strings....
🌐
Vultr Docs
docs.vultr.com › java › examples › check-if-a-string-is-empty-or-null
Java Program to Check if a String is Empty or Null | Vultr Docs
December 17, 2024 - The Java language provides various ways to handle this, ensuring robustness and preventing runtime errors like NullPointerException from derailing your application. In this article, you will learn how to effectively check if a string is empty or null using several methods provided by Java.
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?

Find elsewhere
🌐
Stack Abuse
stackabuse.com › java-check-if-string-is-null-empty-or-blank
Java: Check if String is Null, Empty or Blank
February 28, 2023 - String string = "Hello there"; if (string == null || string.equals("") || string.trim().equals("")) System.out.println("String is null, empty or blank"); else System.out.println("String is neither null, empty nor blank"); In much the same fashion as the before, if the trimmed string is "", it was either empty from the get-go, or was a blank string with 0..n whitespaces: ... The Apache Commons is a popular Java library that provides further functionality.
🌐
Medium
medium.com › @ecetasci.iu › checking-for-null-or-empty-strings-in-java-19518fa1e553
Checking for Null or Empty Strings in Java | by Ece Tasci | Medium
February 25, 2025 - In Java, calling a method on a null reference will result in a NullPointerException, one of the most common runtime errors. To prevent this, it is crucial to check for null before performing operations on a variable. An empty String ("") has zero characters but still exists in memory, specifically in the heap where Java stores objects.
🌐
LabEx
labex.io › tutorials › java-how-to-check-if-a-string-is-null-in-java-559988
How to Check If a String Is Null in Java | LabEx
We use an if statement with the equality operator (==) to check if each string is null. We print a message indicating whether the string is null or not. Save the NullCheck.java file (Ctrl+S or Cmd+S).
🌐
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...
🌐
DEV Community
dev.to › monknomo › finding-null-or-empty-string-checks-in-java › comments
[Discussion] Finding Null or Empty String Checks in Java — DEV Community
May 29, 2017 • May 29 '17 · Copy link · Hide · You should leverage StringUtils.isEmpty(str), which checks for empty strings and handles null gracefully. Learn Java here: hackr.io/tutorials/learn-java ·
🌐
TutorialsPoint
tutorialspoint.com › checking-for-null-or-empty-in-java
Checking for Null or Empty in Java.
We can check whether a particular String is empty or not, using the isBlank() method of the StringUtils class. This method accepts an integer as a parameter and returns true if the given string is empty, or false if it is not.
🌐
Apps Developer Blog
appsdeveloperblog.com › home › java › check if a string is null, empty or blank in java
Check if a String is Null, Empty or Blank in Java
June 4, 2023 - Using the == operator: The == operator in Java compares the memory addresses of two objects. To check if a string is null using this operator, you can compare the string variable with the null literal (null).