The easiest way to check is entity == null. There is no shorter way to do that.

Note that there is a method for this in the standard lib:

Objects.isNull(Object obj)

And another one which is the opposite of the above one:

Objects.nonNull(Object obj)

And there is yet another one which can be used to force a value to be not null, it throws a NullPointerException otherwise:

T Objects.requireNonNull(T obj);

Note: The Objects class was added in Java 7, but the isNull() and nonNull() methods were added only in Java 8.

Answer from icza on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › avoid check for null statement in java
Avoid Check for Null Statement in Java | Baeldung
April 8, 2019 - Without flatMap, the result would be Optional<Optional<String>>. The flatMap operation is only performed when the Optional is not empty. Lombok is a great library that reduces the amount of boilerplate code in our projects. It comes with a set of annotations that take the place of common parts of code we often write ourselves in Java applications, such as getters, setters and toString(), to name a few. Another of its annotations is @NonNull. So, if a project already uses Lombok to eliminate boilerplate code, @NonNull can replace the need for null checks.
🌐
Coderanch
coderanch.com › t › 496486 › java › null-null-java
null and not null in java (Beginning Java forum at Coderanch)
And that makes sense - if test is null that code will throw a NullPointerException. To compare against null, always use "== null" or "!= null". Everything else should be avoided. SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6 How To Ask Questions How To Answer Questions ... a reference having a value null means that it doesn't point to any object.it is simply declaring a refernce variable.but "not null" means that the refernce is pointing to some object and stores "a memory address about how to get to the object in the memory"
🌐
LabEx
labex.io › tutorials › java-how-to-check-if-an-object-is-null-in-java-560011
How to Check If an Object Is Null in Java | LabEx
Using the equality operator (==) is the standard and recommended way to check if a reference variable is null in Java. It's simple, clear, and efficient. In this step, we will learn how to combine checking for null with checking the type of ...
🌐
Quora
quora.com › What-is-the-correct-way-to-determine-if-Java-object-is-null-or-not-in-tasker
What is the correct way to determine if Java object is null or not in tasker? - Quora
Answer (1 of 2): There are two ways you can do null checking of Java objects. The first way, direct from the userguide, is checking of the object [code ]Equals null[/code]: If A Java object can be directly referenced in a condition. Null-value objects are replaced with text representation [code...
🌐
Java2Blog
java2blog.com › home › core java › java basics › check if object is null in java
Check if Object Is Null in Java - Java2Blog
November 29, 2023 - The most basic and efficient way to check if an object is null is by using the == operator. ... Object myObject = null;: This line declares a variable named myObject of the type Object, which is a class in Java.
Find elsewhere
🌐
Medium
medium.com › better-programming › checking-for-nulls-in-java-minimize-using-if-else-edae27016474
Checking for Nulls in Java? Minimize Using “If Else” | by Itır Ege Değer | Better Programming
January 26, 2022 - If you are not familiar with Lombok, I highly suggest you to check it out. I personally love Lombok and it makes a developer’s life much easier :) Lets say that you have Student.javawith fields such as id, name and classes. You can use put @Builder.Default before the related field and give it a default value. When an instance of this Student class is created, it will have “classes” as an empty list, not null.
🌐
Javatpoint
javatpoint.com › how-to-check-null-in-java
How to Check null in Java
How to Check null in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Baeldung
baeldung.com › home › java › core java › check if all the variables of an object are null
Check If All the Variables of an Object Are Null | Baeldung
January 8, 2024 - The ObjectUtils’s allNull() method has a generic API that handles any type and number of parameters. That method receives an array of Objects and returns true if all values in that array are null.
🌐
Liberian Geek
liberiangeek.net › home › how-to/tips › how to check if an object is null in java?
How to Check if an Object is Null in Java? | Liberian Geek
October 20, 2025 - The 'requireNonNull()' method in Java is used to check if an object is null and throw a NullPointerException if it is. It ensures that the object is not null before performing any operations on it.
🌐
Medium
tamerardal.medium.com › simplify-null-checks-in-java-writing-clean-code-with-apache-commons-lang-3-e7d3aea207bd
Simplify Null Checks in Java: Writing Clean Code with Apache Commons Lang 3 | by Tamer Ardal | Medium
September 18, 2024 - if (ObjectUtils.isEmpty(myObject)) { // Null or empty } if (ObjectUtils.isNotEmpty(myObject)) { // Not null or empty } Alternatively, you can also use Java Util.
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › java › how to check null in java (with pictures) - wikihow
How to Check Null in Java (with Pictures) - wikiHow
May 15, 2025 - You can also use “!=” to check that a value is NOT equal. ... Use an “if” statement to create a condition for the null. The result of the expression will be a boolean (true or false) value.
🌐
Educative
educative.io › answers › what-is-objectsisnull-in-java
What is Objects.isNull in Java?
The method isNull is a static method of the Objects class in java that checks whether the input object reference supplied to it is null or not. If the passed object is null, then the method returns true.
🌐
sebhastian
sebhastian.com › java-is-not-null
Java - How to check if a variable or object is not null | sebhastian
March 18, 2022 - You can do so using the not equal operator (!=) like this: String response = getNull(); if(response != null){ System.out.println(response.length()); } When the value of response is not null, then Java will run the println() method inside the if block above. You can also use the Objects.nonNull() method ...
Top answer
1 of 3
5

The dilemma

If a variable with null value gets used in your program causing a NullPointerException, this is clearly a situation in your program which you did not expect. You must ask yourself the question: "Did I not expect it because I didn't take into consideration the possibility of a null value or did I assume the value could never be null here?"

If the answer is the latter, the problem isn't because you didn't handle the null value. The problem happened earlier, and you're only seeing the consequence of that error on the particular line it's used. In this case, simply adding a if (variable != null) isn't going to cut it. You'll wind up skipping lines you were supposed to execute because the variable was null, and you'll ultimately hit a line further on where you again assumed it wouldn't be null.

When null should be used

As a general rule, return null only when "absent" is a possible return value. In other words, your data layer may search for a record with a specific id. If that record isn't found, you can either throw an exception or simply return null. You may do either, but I prefer not to throw exceptions in situations where the strong possibility exists. So you return null instead of a value.

The caller of this method, presumably written by you, knows the possibility exists that the record may not exist and checks for null accordingly. There is nothing wrong with this in this case, though you should handle this possibility as soon as possible as otherwise everywhere in your program you will need to deal with the possibility of a null value.

Conclusion

In other words, treat null as a legitimate value, but deal with it immediately rather than wait. Ideally in your program, you should ever only have to check if it is null once in your program and only in the place where such a null value is handled.

For every value you expect to be non-null, you need not add a check. If it is null, accept that there is an error in your program when it was instantiated. In essence, favor fail fast over fail safe.

2 of 3
8

Deciding whether or not null is a allowed as an object value is a decision that you must make consciously for your project.

You don't have to accept a language construct just because it exists; in fact, it is often better to enforce a strict rule against any nullvalues in the entire project. If you do this, you don't need checks; if a NullPointerException ever happens, that automatically means that there is a defect in your code, and it doesn't matter whether this is signalled by a NPE or by some other sanity check mechanism.

If you can't do this, for instance because you have to interoperate with other libraries that allow null, then you do have to check for it. Even then it makes sense to keep the areas of code where null is possible small if possible. The larger the project, the more sense it makes to define an entire "anti-corruption layer" with the only purpose of preserving stricter value guarantees than is possible elsewhere.

🌐
Linux Hint
linuxhint.com › check-object-is-null-java
How to Check if an Object is Null in Java
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Delft Stack
delftstack.com › home › howto › java › java check if object is null
How to Check if an Object Is Null in Java | Delft Stack
February 12, 2024 - The equals method is part of the Object class in Java and is used for comparing the contents of objects for equality. When applied to a non-null object, it returns true if the compared objects are equal and false otherwise. However, when applied to a null object, it results in a NullPointerException. To perform a null check using equals, it is essential to ensure that the object reference is not ...