Tips to fixing a NullPointerException in Java (Android)

22Mar, 2019


In this post, we are going to talk about the NullPointerException in Java.   We will discuss the main causes and solution to it.   I will also discuss how to track down a NullPointerException in Android Studio.   I will try to explain at a high level (no low-level explanations) so it can be easily understood.

What causes a NullPointerException?

A NullPointerException is usually thrown when you try to access a field or a method in a variable or an object that is null. This is simple to understand however, it might be a bit confusing for some programmers.

Now, the next question is:


What causes an object to be null?

When you create a variable or an object without instantiating it, then the value of that variable or object is null.  So for example, when you have the following code:

The value of firstName in this case would be null.

Now if you try to call a method on the variable firstName, then the NullPointerException will be thrown.

For example:

The above code will throw a null pointer exception on line 2.

Similarly, if you create an object without equating the object, the ugly null pointer exception will be thrown.

For example:

The above code will throw the NullPointerException.

 

How to solve the NullPointerException.

To solve the NullPointerException, you simply need to assign a value to the variable involved. If it is an object, you need to instantiate that object with the new keyword.

For example:

The above code will work swiftly.

For objects:

The above code will also work beautifully.

Debugging the NullPointerException in Android

There are times when even professional developers can swear that the object involved cannot just be null. Personally, I used to be guilty of this. There are times when it just seems impossible for the object in question to be null. However, what I would like to assure you is that when your IDE throws the null pointer exception, be sure your IDE is not mistaken :).

So I will list a couple of scenarios that could result in an object being null even when the object involved seems to be not null.  Please note that there are hundreds of other scenarios, however, I would discuss just a couple of them.

For simplicity, we will use only objects as examples

Scenario 1:

Sometimes, you might have instantiated a class correctly, but later in your code, unknowingly assigned a null value to that object.

For example:

The above example is an illustration of how a NullPointerException can be caused by “programmer error”.

Scenario 2:

Calling findViewById() in an Activity class whose layout does not contain the requested view.

This is one of the most common reasons developers encounter the NullPointerException when developing an Android App.   Take a look at the code sample below.

The code above is valid and should not throw any exceptions under normal conditions. However, the id R.id.imgHeartLoading has to be present in the layout file R.layout.activity_splash.  If the requested view is not present in the R.layout.activity_splash layout file, then the findViewById() method will return null. In a scenario where findViewById() returns null, then the next line pb.setOnClickListener(new View.OnClickListener() {...}); would throw the NullPointerException.

Basically, when you call the findViewById() method in an Activity class, the method tries to locate the requested view in whatever layout that was passed to setContentView().   Some developers assume that the findViewById() method goes through every single layout file in the project, however, this is very wrong.

So in a case like this:

  • The variable pb is being equated to findViewById(R.id.imgHeartLoading)
  • R.id.imgHeartLoading exist somewhere in the project
  • But the variable pb is still null.

Tips to avoid NullPointerException

NullPointerException can be avoided in programming by using the following

1.   Check for null values before calling an object’s method:

This is a very simple solution.   In the example above, we can avoid the NullPointerException by inserting an if statement just before calling setOnClickListener

For Example:

In this case, the if(){} block will never be executed if the pb variable is null

2.   Use a try{} catch(){} Block:

This solution simply involves wrapping your code in a try and catch block.

For Example:

 

Note:

The above solutions are just temporary solutions.   These solutions only avoid the NullPointerException but they do nothing to solve the problem.   As a good developer, you should track down the main reason for the exception.

And that’s all for now.

If you have any questions or contributions, feel free to post them in the comment box below :).


Leave a Reply

Your email address will not be published. Required fields are marked *