Skip to main content

NullPointerException in Java – Explaining the Billion Dollar Mistake

Null Pointer Exception Java

Credit: Adam Sinicki / Android Authority

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?



source https://www.androidauthority.com/null-pointer-exception-1156263/

Comments

Popular posts from this blog

5 tips to Voice Speech Recognition in Android Marshmallow

Android Marshmallow landed on our Android devices. The opportunity for us to offer a small compilation of tricks to try immediately. The latest version of the Google OS starts (very gently, unhurriedly) to be offered on other devices as Nexus. You will find on Android TM, in the coming weeks, a compilation of the best tips for Android 6.0 Marshmallow. It starts slowly with a video featuring the 10 essential tips & tricks of this new version of the little green robot OS. To continue, we offer a selection of five "tricks" on the management of the battery on Android 6.0 Marshmallow. To enjoy longer your newly updated mobile. Follow the guide. then continue with 5 tips to tame the super-assistant Google Now on Tap. Here you will find 5 "tips" to manage in the best way your applications. We then discuss the quick tips to navigate more easily on this version of the Google OS. We enchanters with features focused on safety and the protection of personal data. We co...

Energy Android TV Play turns your TV into a Smart TV

ENERGY SISTEM Android Energy TV Play, you have a smart TV with Android operating system allows you to convert any traditional TV has announced the launch of a new product. Energy Android TV Play can be connected to the TV to enjoy f the size of a flash drive, a smart phone, a tablet and a computer unconsolidated is a lightweight device. System 1.6 GHz, DDR3 1GB of RAM and a dual-core processor can be expanded using external USB devices, which is the internal memory of 8 GB. It also integrates WiFi and a USB port for connecting external devices. One of its outstanding features, it is easily connected to the TV screen by screen cast application to display the contents of any terminal, making any phone or tablet is synchronized with iOS or Android. All ENERGY SISTEM products one click In addition, through streaming media service applications, images, video or other multimedia content, and game play is the ability to share. With integrated WiFi, the device you want from t...

How to run Python apps on any platform

Credit: Adam Sinicki / Android Authority Want to know how to run Python? It sounds simple, but it can actually be tricky to figure this out. In this post, we’ll discuss how to test your Python code, as well as how to run Python in other contexts: online for example, or as a packaged app. Sometimes, the thing holding you back from learning to code can be extremely simple. I remember wanting to learn to program when I was younger – or learning to take what I’d learned from BASIC on the ZX Spectrum and apply that to a modern environment. My problem? I didn’t know “where” to program. Once I understood C# or Java, where would I enter the code and how would I run it? And whenever I asked someone, they would look at me blankly. What kind of a question is that? Thing is, I had never needed an IDE or an interpreter before. Machines like the ZX Spectrum and Tatung Einstein (any other Einstein users out there?) simply booted up with a prompt to code into! Many people have a similar iss...