Affiliate links on Android Authority may earn us a commission. Learn more.
NullPointerException in Java - Explaining the billion dollar mistake
While Java is one of the most in-demand and widely used programming languages in the world, it is not without its detractors. Java is not a particularly beginner-friendly language and it is rife with rigid rules and structures that can be tough to wrap your head around. Then there’s the NullPointerException.
Null pointer exception is an “exception” (error) that is particularly common when programming in Java. For a beginner, this is a confusing and daunting message to receive, but it can be just as much of a headache for pros to deal with! In fact, null pointer exceptions are so common and so damning, that they are referred to as “the billion-dollar mistake.” Ouch!
In this post, we’re going to unwrap the phrase to discover exactly what it means and what you should do when you encounter a null pointer exception in your own code!
What does null pointer exception mean?
First, we need to understand precisely what is meant by a pointer and how it can be null!
To do this, it helps to remember that a variable is actually a “pointer” that tells the computer to look at a certain location. That location will then contain a value, which is the value of the given variable.
So, if we say “int x = 3” then the computer will choose a location to call “x” and it will store the value “3” there. Whenever you refer to x further on in your code, the computer will look at that location and see that it is a “3” (unless you changed it). The variable points the computer to the right destination.
See also: Understanding variables in Java
This is why it’s useful to know the potential size of a variable first, as it tells the computer how much space to allocate for that value.
But what happens when you have a pointer that doesn’t point anywhere? That’s where the null pointer exception comes in.
Objects and references
An integer like “x” is what we call a “primitive type.” That means that it is a fundamental value.
An object, on the other hand, is a more complex data structure that can have multiple values and methods. Like primitive types, however, they still need to be stored somewhere and they still need an address and a pointer.
An example is String. This is not a primitive type in Java, but rather a reference type: in this case it “points” to a class that can have multiple values and methods. Once you have initialized an instance of a string, you can do things like:
myString.length();
This will tell you how long said string is by using the method that belongs to that class. But this is also what makes the null pointer exception possible because “length()” is a value that refers to a specific instance of an object. That means it acts on a specific string once it has been created.
Thus, you need to say:
String myString = “Hello”;
myString.length();
But were you to say:
String myString = null;
myString.length();
You would have created a pointer (myString) but that pointer does not point anywhere – it doesn’t know where to look. Another way to say this? The pointer is null.
The key thing to understand is that it is not that there is no value, but rather that the variable isn’t pointing anywhere for there to be a value.
There are certain coding design patterns that benefit from the ability to assign null values, so it’s a useful feature in those circumstances. As long as you remember that the object is null!
How to prevent null pointer exceptions Java
The reason that null pointer exceptions are so unpopular in Java is that they are runtime exceptions. That means the app will compile just fine and you’ll only hear about them when you reach that point in the code. It is, therefore, possible for a null pointer to slip through the net if you are initializing lots of objects on the fly.
This is why it’s useful to include measures to prevent null pointer exceptions during runtime.
The easiest way to do this?
if (myString == null) {
return;
}
This will help you to quickly check if an object is null, thereby circumventing code that would throw an exception. There are other tricks you can use, too. For example, when comparing strings, it’s a good move to invoke the method from the string literal, rather than the other way around:
If(“Hello!”.equals(myString)) {
Rather than:
If(myString.equals(“Hello!”)) {
You will avoid throwing an exception this way. OR you could just use Kotlin, which is “null safe” by design!
See also: Kotlin tutorial for Android for beginners: Build a simple quiz
Want to learn more about writing well-designed, flawless Java code? Then check out our guide to the best resources to learn Java. Or why not get started with our free, comprehensive Java tutorial for beginners?