What Is A NullPointerException, And How Do I Fix It? In Java

NullPointerExceptionIn this tutorial, We will learn What Is A NullPointerException, And How Do I Fix It? In Java As we know that exceptions occur when there is any condition that may cause some unavoidable error or Situation when the program can not proceed anymore. And handling the such situation are called exception handling.

There are a number of exceptions that may occur during execution so there are corresponding ways to handle them that we call exception to handle and from those NullOintException is also a type of exception when the control cannot find the appropriate object where is it referred.

Or we can say either the object has a null value or there is no such object that exists. To understand better follow the tutorial.

Types Of Variables

There are two main types of variables in Java:

Primitive Variables

All the that we use to store simple values are known as primitives for example int, Char, array, etc. And the main thing about these types of variables is that if you want t manipulate the values in the store you can do so directly, Moreover, the variable starts with a lowercase.

References

They are the pointers that store the Memory address of an object, Which means a variable is referring to an object which means the address of an object is stored in that variable.

NullPointerException

This an exception that occurs when a variable referee to an objection there is no such object exists or that variable is referring to a null value in that condition we can not find the right object in fact no object will be found in that case so there are ways to handle the situation.

Let’s discuss ways to handle that exception or ways to fix such a problem.

Handling NullPointerException

public class Printer {
    private String name;

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

    public void print() {
        printString(name);
    }

    private void printString(String s) {
        System.out.println(s + " (" + s.length() + ")");
    }

    public static void main(String[] args) {
        Printer printer = new Printer();
        printer.print();
    }
}
Exception in thread "main" java.lang.NullPointerException
    at Printer.printString(Printer.java:13)
    at Printer.print(Printer.java:9)
    at Printer.main(Printer.java:19)

 

The above code shows the example of NullPointerException and a way to handle it. As in print String, the line will get an exception

Look at the line and check which values are null by adding logging statements or using a debugger. We find out that s is null, and calling the length method on it throws the exception. We can see that the program stops throwing the exception when s.length is removed from the method.

Let’s look in to the values

Next check where this value comes from. By following the callers of the method, we see that s is passed in with print(string name) in the print method, and this name is null.

Now let’s get the exception

Where is this.name set? In the set.name(String) method. With some more debugging, we can see that this method isn’t called at all. If the method was called, make sure to check the order that these methods are called, and the set method isn’t called after the print method.

This is enough to give us a solution: add a call to print.setname() before callingPrinter.print().

 

Learn more about NulPointerExcpeption at: Python for all

and also visit JAVA TUTORIAL for exceptional handling

Leave a Comment

%d bloggers like this: