Check if an Object Is Null in Java

Rupam Yadav Jan 30, 2023 Dec 13, 2020
  1. Java Check if Object Is Null Using the == Operator
  2. Java Check if Object Is Null Using java.utils.Objects
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.

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.

public class JavaCheckNullObject {

    public static void main(String[] args) {

        User2 user;
        user = new User2();

        User1 getUserObject = user.getUser1Object();

        if (getUserObject == null) {
            System.out.println("Object is Null");
        } else {
            System.out.println("Not Null");

            getUserObject.setName("Sam");
            System.out.println(getUserObject.getName());
        }
    }

}

class User2 {

    User1 user;

    public User1 getUser1Object() {
        return user;
    }
}

class User1 {
    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Output:

Object is Null

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.

import java.util.Objects;

public class JavaCheckNullObject {
    public static void main(String[] args) {
        User2 user;
        user = new User2();

        User1 getUserObject = user.getUser1Object();

        if (Objects.isNull(getUserObject) ){
            System.out.println("Object is Null");
        } else {
            System.out.println("Not Null");

            getUserObject.setName("Sam");
            System.out.println(getUserObject.getName());
        }
    }
}
class User2 {

    User1 user;

    public User1 getUser1Object() {
        return user;
    }
}

class User1 {
    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Output:

Object is Null
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Object