Check if an Object Is Null in Java
This tutorial will go through the methods to check if an object is null in Java with some briefly explained examples.
Please enable JavaScript
Java Check if Object Is Null Using the == Operator
As an example, we have created two classes — User1 and User2 . The class User1 has one instance variable name and the Getter and Setter methods to update and retrieve the instance variable name . The User2 class has one method, getUser1Object , which returns the instance of class User1 .
In the main method, we create an object of the User2 class named user and call the getUser1Object() on it, which returns the instance of the class User1 . Now we check if the instance of the User1 class returned by the method is null or not by using the == operator in the if-else condition.
If the object returned is not null , we can set the name in the User1 class by calling the setter method of the class and passing a custom string as a parameter to it.
Java Check if Object Is Null Using java.utils.Objects
The java.utils.Objects class has static utility methods for operating an object. One of the methods is isNull() , which returns a boolean value if the provided reference is null, otherwise it returns false.
We have created two classes — User1 and User2 as shown in the code below. In the main method, we created an object of the User2 class using the new keyword and called the getUser1Object() method. It returns an object of class User1 , which we later store in getUser1Object .
To check if it is null, we call the isNull() method and pass the object getUserObject as a parameter. It returns true as the passed object is null.
How to Check if an Object is Null in Java
Java is a dynamic object-oriented programming language that implements classes and objects. A unique instance of a class defines an object of the class. It is a self-contained entity with a state and behavior that facilitates mapping real-world entities while coding. The class defines the data and methods, and its object can utilize them to represent a specific entity.
This article will demonstrate the methods to check if the object is null in Java.
How to Check if an Object is null in Java?
For checking whether the object is null or not, you can use:
We will now implement each of the mentioned methods, one by one!
Method 1: Check if an Object is null in Java Using Comparison Operator
In Java, the comparison operator “==” is mostly used to compare two entities. It returns true or false after performing the comparison. This operator can also be utilized to determine whether an object is null or not.
Syntax
The syntax for verifying an object is null using the Comparison Operator is given below:
Example
In this example, we have two classes named “myFirstClass” and “objectCheckExample”. The “myFirstClass” contains an empty constructor, that is called when the object or instance of the class is instantiated:
Here, we will create an instance of the “myFirstClass” in the main() method of “objectCheckExample” class and then we will check either the object is null or not by adding the comparison operator “==” in the “if” statement:

The output shows the object “myClass1” is null because we have only declared it. Without instantiation, the object is considered null:

Now, let’s confirm whether the object is null or not when it is instantiated.
Method 2: Check if an Object is null in Java Using isNull() Method
Another method to check whether an object is null or not is the “isNull()” method. It is a static method of the Objects class. It receives an object as an argument and outputs the boolean value true or false.
Syntax
Follow the below given syntax for “isNull()” method:
Here, “myClass1” object will be validated using the “isNull()” method.
Example
We will create an instance of “myFirstClass” in the main() method of the class named “objectCheckExample”. Using the “new” keyword, the object will be declared and instantiated simultaneously. After that, check whether the object is null or not with the help of the “isNull()” method. As this is a static method so, it will be called by using the class name “Objects”:

The output indicates that the object of class “myFirstClass” is not null because the object is instantiated:

Let’s check the other ways to verify the object is null or not.
Method 3: Check if an Object is null in Java Using nonNull() Method
We can also verify whether the object is null or not with the help of the “nonNull()” method. It is also a static method belonging to the Objects class. It also takes an object as a parameter and returns a boolean value where true means the object is not null.
Syntax
Here, the syntax for the method is given:
The negation (!) operator is used to convert the result of the “nonNull()” method so that it returns false if the object is not null.
Example
In our “myFirstClass”, we will now create a String type variable “Name” and a parameterized constructor that takes “name” as a parameter:
In the main() method of the “objectCheckExample” class, pass the name “John” as an argument to the created object. After that we will verify the object by using the “nonNull()” method:

As you can see, the object is not null because we have assigned value to its “Name” property:

Let’s check one more method to verify the object is null or not.
Method 4: Check if an Object is null in Java Using requireNonNull() Method
The “requireNonNull()” method is a static method and belongs to the Objects class. It takes the class object as an input in the method. If the object is null, an exception is thrown.
Syntax
Following described syntax is used for the “requireNonNull()” method:
Example
We will verify whether the created object “myClass1” is null or not by using the “requireNonNull()” method. Here, we will add a try-catch block to handle the exception.
In the try block, we call the “requireNonNull()” method and pass the “myClass1” object to it. It will print the specified line if the object is not null. Otherwise, it goes to the catch block and throw a null exception by printing the given statement:

The resultant output shows that the object is not null because it contains a value:

We have provided all the essential information about how to check an Object is null in Java.
Conclusion
To verify if the object in Java is null or not, you can use different methods: Comparison Operator, isNull() method, nonNull() method, and requireNonNull() method. It is a good practice to verify whether the object is null or not while coding; otherwise, you can face failures and unexpected outputs. This article demonstrated the methods to determine if an object is null in Java.
About the author

Farah Batool
I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.
Java — How to check if a variable or object is not null
Java will throw the NullPointerException error when you call a method on an object with the value of null .
This frequently happens when you call methods from an object returned from a function as follows:
The call to the response.length() in the code above is done on a String variable with a null value, causing Java to throw the NullPointerException error:
To avoid the error and make the code run without any error, you can perform an if check on the returned value response and see if the value is null.
You can do so using the not equal operator ( != ) like this:
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 to check whether a variable is not null .
You need to import the Objects class and use the nonNull() static method as follows:
Finally, you can also add an else condition to define code that will be executed when the result is null:
The full code for is not null check is as shown below:
To summarize, you can check whether a variable or object is not null in two ways:
- Using the not equal operator ( variable != null )
- Using the Objects class nonNull() method
Checking for null values before executing further operations will help you to avoid the NullPointerException error.
And that’s how you check if a variable or object is not null in Java. I hope this tutorial has been useful for you
Level up your programming skills
I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

report this ad
About
Sebhastian is a site that makes learning programming easy with its step-by-step, beginner-friendly tutorials.
Learn JavaScript and other programming languages with clear examples.
Free Code Snippets Book
Get my FREE code snippets Book to 10x your productivity here

report this ad
Java: How to check if object is null?
I am creating an application which retrieves images from the web. In case the image cannot be retrieved another local image should be used.
While trying to execute the following lines:
The line if(drawable.equals(null)) throws an exception if drawable is null.
Does anyone know how should the value of drawable be checked in order not to throw an exception in case it is null and retrieve the local image (execute drawable = getRandomDrawable())?
![]()
8 Answers 8
The equals() method checks for value equality, which means that it compares the contents of two objects. Since null is not an object, this crashes when trying to compare the contents of your object to the contents of null .
The == operator checks for reference equality, which means that it looks whether the two objects are actually the very same object. This does not require the objects to actually exist; two nonexistent objects ( null references) are also equal.
Edited Java 8 Solution:
You can declare drawable final in this case.
As Chasmo pointed out, Android doesn’t support Java 8 at the moment. So this solution is only possible in other contexts.
I use this approach:
This way I find improves the readability of the line — as I read quickly through a source file I can see it’s a null check.
With regards to why you can’t call .equals() on an object which may be null ; if the object reference you have (namely ‘drawable’) is in fact null , it doesn’t point to an object on the heap. This means there’s no object on the heap on which the call to equals() can succeed.
![]()
if (yourObject instanceof yourClassName) will evaluate to false if yourObject is null .
![]()
The above line calls the «equals(. )» method on the drawable object.
So, when drawable is not null and it is a real object, then all goes well as calling the «equals(null)» method will return «false»
But when «drawable» is null, then it means calling the «equals(. )» method on null object, means calling a method on an object that doesn’t exist so it throws «NullPointerException»
To check whether an object exists and it is not null, use the following
In above condition, we are checking that the reference variable «drawable» is null or contains some value (reference to its object) so it won’t throw exception in case drawable is null as checking