All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Handle Null Pointer Exception In Java

Last Updated : Mar 11, 2024

How To Handle Null Pointer Exception In Java

In this article we will show you the solution of how to handle null pointer exception in java, NullPointerException's runtime issue occurs when a variable is queried but doesn't point to anything, rather referring to nothing or null.

Due to the fact that the NullPointerException is a runtime problem, it does not need to be explicitly caught and handled in application code.

A NullPointerException is raised whenever an application code attempt is made to access or change an uninitialized object.

The object reference basically means that it points nowhere and has a null value.

We will now discuss the idea of how to handle null pointer exception in java with an example.

Step By Step Guide On How To Handle Null Pointer Exception In Java :-

import java.util.UUID;
import java.io.*;
class talkerscode
{
    private static Singleton single = null;
    private String ID = null;
    private Singleton()
    {
        ID = ID.randomID().toString();
    }
    public static Singleton getInstance()
    {
        if (single == null)
            single = new Singleton();
        return single;
    }
    public String getID()
    {
        return this.ID;
    }
}
public class TalkersCode
{
    public static void main(String[] args)
    {
         s = Singleton.getInstance();
        System.out.println(s.getID());
    }
}
  1. First, a class called "Singleton" with a private constructor is created. This implies that we are unable to make an instance of this class from somewhere else.
  2. The single instance of the class will be stored in the private static variable "single" of type Singleton, which we define.
  3. A class instance can be obtained only by calling the public static method "getInstance()," which we define next. In the event of a null "single", a new instance is created and assigned to it.
  4. We simply return the currently active instance if "single" is not null.
  5. Additionally, we define a private instance variable called "ID" that receives a unique ID from the constructor using the UUID class.
  6. We create a public method called "getID()" that returns the "ID" value.
  7. Using the "getInstance()" function in the "TestSingleton" class, we generate a fresh instance of Singleton and save it in the variable "s."
  8. The result of "s.getID()," which ought to be a special identifier for this instance of the Singleton class, is then printed out.
  9. The constructor is private, so if we try to use it to create another instance of the Singleton class, we will encounter a compilation problem. Instead, we must use the getInstance() function to get the class's sole instance.

Conclusion :-

As a result, we have successfully learned how to handle null pointer exception in java with an example.

A runtime exception in Java is the null pointer exception. When a software attempts to use a reference to an object that is set to null, a null pointer exception occurs.

The null pointer exception in Java can happen in a variety of situations, such as when we use a null object to access a method or specific field or when we utilize a null value in a synchronized block.

I hope this article on how to handle null pointer exception in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪