NullPointerException Java Exception Handle – Understand and Fix

NullPointerException Java Exception Handle - Understand and Fix

NullPointerException Java

“NullPointerException Java” is one of the most common errors that occur so often among beginners and intermediate level coders. Sometimes it even bothers the most experienced developers. When you declare a reference variable (i.e. an object) you’re very making a pointer to an object. Consider the following code wherever you declare a variable of primitive type int:

int x;
x = 10;

In this example, the variable x is an int and Java can initialize it to 0 for you. When you assign it the value of 10 on the second line, your value of 10 is written into the memory location referred to by x.
But, once you attempt to declare a reference type, one thing totally different happens. Take the following code:

Integer num;
num = new Integer(10);

The first line declares a variable named num, but it does not actually contain a primitive value yet. Instead, it contains a pointer (because the type is int that is a reference type). Since you’ve got not however said what to point to, Java sets it to null, which means “I am pointing to nothing”.
In the second line, the new keyword is used to instantiate (or create) an object of type Integer and the pointer variable num is assigned to that Integer object.

NullPointerException Java – Why Does It Occur?

The NullPointerException Java happens after you declare a variable, however, it did not produce an object. So you are pointing to something that does not actually exist.
If you try to dereference num BEFORE making the object you get a NullPointerException Java. In the most trivial cases, the compiler will catch the problem and let you know that “num may not have been initialized,” but sometimes you may write code that does not directly create the object.
For instance, you may look at a method as follows:

public void doSomething(SomeObject obj)

In which case, you are not creating the object obj, but rather assuming that it was created before the doSomething() method was called. Note, it is possible to call the method like this:
doSomething(null);
In which case, obj is null. If the tactic is meant to try and do one thing to the passed-in object, it is appropriate to throw the NullPointerException Java because it’s a programmer error and the program will need that information for debugging purposes.

Conclusion

Alternatively, there are also cases where the purpose of the method isn’t only to control on the passed in object, and thus a null parameter could also be acceptable. In this case, you’d got to check for a null parameter and behave differently. You should also explain this in the documentation. For example, doSomething() could be written as:

/**
* @param obj An optional foo for ____. May be null, in which case
* the result will be ____.
*/
public void doSomething(SomeObject obj) else
}

Please wait...

Subscribe For Something Amazing Everyday

Want to be notified when our article is published? Enter your email address and name below to be the first to know.