Solving Null Pointer Exception in Java
Java is one of the most popular and widely used programming languages in the world, but not without its critics. Java is not a particularly beginner-friendly language, and there are many strict rules and structures that are difficult to understand. Then there is the NullPointerException.
The null pointer exception is an “exception” (error) that occurs particularly frequently when programming in Java. For a beginner, this is confusing and daunting news, but it can be a headache for professionals as well! In fact, null pointer exceptions are so common and so harmful that they have been referred to as “billion dollar errors.” Ouch!
In this post, we’re going to unpack the expression to find out what it exactly means, and what to do if you encounter a null pointer exception in your own code!
What does a null pointer exception mean?
First we need to understand exactly what is meant by a pointer and how it can be zero!
To do this, it is helpful to remember that a variable is actually a “pointer” that tells the computer to look at a particular location. That location then contains a value that is the value of the specified variable.
So when we say “int x = 3”, the computer chooses a location to call “x” and stores the value “3” there. If you refer to x in your code below, the computer will check that location and see that it’s a “3” (unless you changed it). The variable Points the computer to the right destination.
See also: Understand variables in Java
For this reason, it is helpful to know the potential size of a variable first, as it tells the computer how much memory to reserve for that value.
But what if you have a pointer that is pointing nowhere? This is where the null pointer exception comes into play.
Objects and references
An integer like “x” is what we call the “primitive type”. That said, it’s a fundamental value.
An object, on the other hand, is a more complex data structure that can have multiple values and methods. However, like primitive types, they still need to be stored somewhere and they still need an address and a pointer.
One example is String. This is not a primitive type in Java, but a reference type: in this case, it refers to a class that can have multiple values and methods. Once you’ve initialized an instance of a string, you can do the following:
myString.length();
Here you can see the length of the string using the method that belongs to this class. However, this also makes the null pointer exception possible, since “length ()” is a value that relates to a specific instance of an object. That is, it acts on a specific string once it was created.
So you have to say:
String myString = “Hello”; myString.length();
But should you say:
String myString = null; myString.length();
You would have made one pointer (myString), but this pointer is pointing nowhere – it doesn’t know where to look. Another way of saying that? The pointer is zero.
The most important thing to understand is that there is no value, but that the variable is not pointing to it anywhere Be a value.
There are certain coding design patterns that benefit from the ability to assign null values. Therefore, this is a useful feature in these circumstances. As long as you remember that the object is null!
How to Prevent Null Pointer Exceptions Java
The reason null pointer exceptions are so unpopular in Java is that they are runtime exceptions. This means the app will compile just fine and you won’t know about it until you get to this point in the code. It is therefore possible that a null pointer slips through the network if you initialize many objects during operation.
For this reason, it is helpful to include measures to prevent null pointer exceptions at runtime.
The easiest way to do this?
if (myString == null) { return; }
This is a quick way to verify that an object is null and bypass code that would throw an exception. There are other tricks you can use as well. For example, when comparing strings, a good way to do this is to call the method from the string literal rather than the other way around:
If(“Hello!”.equals(myString)) {
Rather than:
If(myString.equals(“Hello!”)) {
This way you avoid throwing an exception. OR you could just use Kotlin, which is inherently “zero safe”!
See also: Kotlin Tutorial for Android for Beginners: Create a Simple Quiz
Want to learn more about writing well-designed, error-free Java code? Then check out our guide to the best resources to learn Java. Or why not start with our free, comprehensive Java tutorial for beginners?