Java Null Pointer Exception Basics for Beginners

java null pointer exceptionLooking at the title of this article, if you have started associating it with Null Pointers, I would urge you against doing that. Null Pointer Exception has nothing to do with Null Pointers.

Null Pointer Exception

Null Pointer Exception occurs when Null reference is used in place where the requirement is for an object reference.

The familiarity of Null Pointer Exceptions with Java Programmers is like the Seven Wonders of the World to a world traveller! As soon as you start making your way into Java programming, the first thing you will probably come face to face with is the Null Pointer Exception. Let’s discuss the details of Null Pointer Exceptions, their causes and how one can resolve a Null Pointer Exception. However, to know all these, you must have a working knowledge of Java. To get to this point, you can take a course such as Basics of Java.

Prior to looking at the details of Java Null Pointer Exceptions, Let us first understand its naming methodology. According to experts, the phenomenon Java Null Pointer Exception is not named appropriately; instead, it should have been named Null Reference Exception. I mean, there is no reference to pointers in Java language even in passing; then how relevant is it to name it a Null Pointer Exception? I hope you will understand this point once we complete the understanding of Null Pointer Exception in this article.

Reason for Null Pointer Exception’s Popularity

The Null Pointer Exception became famous because its error description never gave an idea about a null variable in the code, due to which the exception actually occurred. The error report mentions only the line number where the issue is found. Subsequently, Java beginners have to spend lot of time figuring out the exact cause behind the error. So, it is important for Java beginners to take a basic course such as Java Fundamentals to avoid wasting time on such petty errors.

Moreover, Java Null Pointer Exception is a Run time error. Instead, Null Pointer Exception occurrences in the code should actually be reported during the code compilation. There are few good languages out there in the market which report Null Pointer Exception at compile time to avoid such embarrassments at runtime.

Cases of Null Pointer Exceptions

As mentioned in JAVA’s API documents, Null Pointer Exception is thrown when the null value is used instead of using a reference value. This essentially means that a part of code is trying to access a reference without a value. The major scenarios of such cases are:

1. Trying to call an instance of a null object and modifying or accessing fields with the Null object

2. Passing Null to a function where real value of some kind of reference value is required

The summary of the above two scenarios is that the Null object is used inappropriately.

When an object is created without initialization, it is called a Null object, and the use of such null objects can become a cause of Null Pointer Exceptions. At this point, you may choose to take an online course and learn Java from Scratch in order to learn the basics of Java primitives and their initialization.

Solution to Java Null Pointer Exception

Can a simple use of null check resolve the issue, as shown in the below code?

Object NullObj = getTheNullObject();
if (NullObj != null)
{// to avoid NullPointerException

If you think that resolving the issue of Null Pointer Exception is as simple as it seems in the above code, you are wrong. Just think of a situation where you are working with third party components. How many ‘if’ checks will you use to handle Null Pointer Exception? It will simply spoil your coding standards. Every time you deal with a third party component with poor documentation (generally, it is the case!), you are left with no choice but to use this way out.

The better way to avoid Null Pointer Exceptions is to have better software design and checks at a very high level. For an example, try and use the checks of necessary fields at the UI level when there is a need for business logic to perform an operation on an object. This way, you can avoid using ‘if’ and ‘null’ checks in your code. These are pretty important concepts to learn, and you will come across these points when you take a course such as: Learn Programming in Java.

If you have observed, there is a concept of default value in Java for all the primitives. Really? Then why not use that? In that case, Null will not have any existence and the phenomenon called Null Pointer Exception will not exist at all. In fact, why do we even have Null in Java at first place? Basically, we have to use Null value in cases where information for a particular object is not available.

Some may argue that even zero is treated as information absence. Can we use zero in place of null? The answer is simply, No. Just like zero cannot be used in places where there is a null set in linear algebra (mathematics), default values cannot be used in place of null in Java. This definitely is not a solution to Null Pointer Exceptions. It is a Java programmer’s responsibility to ensure that null objects are not used in the code and, instead, each reference be initialized before its use.

Points to Ponder for Java Null Pointer Exceptions:

1.    Try and trim the usage of Null at the design level. Do not allow the use of null values in the objects where the business scenario is not allowing it to.

2.    Do not miss to use the ‘if’ checks for Null values when you are exporting objects from third party and you do not have control over their code. In this method, use the string constant as left operand while comparing the value of a string variable.

3.    Do not declare variables within constructors. That will cause the instance variable to be hidden.

4.    Arrays are the main culprits of Null Pointer Exception. Hence, make it a practice to initialize the arrays immediately after their declaration.

5.    Try and make a practice of declaring variables just where you are using them. This will help avoid instances of declaring variables with Null values.