Java

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:

classObject == null

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:

class myFirstClass{
    myFirstClass() { }
}

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:

public class objectCheckExample {
    static myFirstClass myClass1;
    public static void main(String[] args) {
         if(myClass1==null)
           System.out.println("The object of the class named 'myFirstClass' is null");
         else
           System.out.println("The object of the class named 'myFirstClass' is not null");
    }
}

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:

Objects.isNull(myClass1)

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”:

public static void main(String[] args) {
         myClass1 = new myFirstClass();
         if(Objects.isNull(myClass1))
           System.out.println("The object of the class named 'myFirstClass' is null");
-        else
           System.out.println("The object of the class named 'myFirstClass' is not null");
    }
}

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:

!Objects.nonNull(myClass1)

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:

class myFirstClass{
    String Name;
    myFirstClass(String name)
    {
        Name = name;
    }
}

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:

public class objectCheckExample {
    static myFirstClass myClass1;
    public static void main(String[] args) {
         myClass1 = new myFirstClass("John");
         if(!Objects.nonNull(myClass1))
           System.out.println("The object of the class named 'myFirstClass' is null");
         else
           System.out.println("The object of the class named 'myFirstClass' is not null");
    }
}

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:

Objects.requireNonNull(myClass1);

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:

public class objectCheckExample {
    static myFirstClass myClass1;
    public static void main(String[] args) {
         myClass1 = new myFirstClass("John");
         try
            {
                Objects.requireNonNull(myClass1);
                System.out.println("The object of the class named 'myFirstClass' is not null");
            }
            catch (NullPointerException e)
            {
                System.out.println("The object of the class named 'myFirstClass' is null");
            }
    }
}

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.