Solving Null Pointer Exception in Java

Must read

How to disable and delete Samsung Pay from your Galaxy phone

When it comes to paying conveniently, Samsung Pay makes using your phone as a bank card an easy process; however, there may come...

How to Watch India vs Australia Live Stream

India's tour of Australia began on Friday with the first of three one-day internationals in Sydney — the city will also host the...

AMD Radeon RX 6800 XT GPU Price in India Revealed, Will Cost Rs. 64,990 Plus GST

AMD Radeon RX 6800 XT graphics card will cost Rs. 64,990 (SEP) in India before GST, the company shared through a post on...
Bhawani Singh
I am a blogger who believes in delivering latest tech news from around the world to my viewers.

Null Pointer Exception Java

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:


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”;

But were you to say:

String myString = null;

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) {

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 link

More articles

Leave a Reply

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

- Advertisement -

Latest article

SpaceX’s Launch Of Sentinel-6 Is A First Step Toward Leveling Up Climate Studies

Saturday's SpaceX launch may be a small step for a company that is making increasingly regular trips into orbit, but the...

How green and profitable are e-scooters?

Wayne Ting, chief executive of e-scooter firm Lime, says there's room for improvement. Source link

How to Change the Theme and Accent Color of Instagram DMs

Khamosh PathakFacebook has merged Instagram DMs with Facebook Messenger. Once you opt-in, you can message your Facebook friends right from Instagram. More importantly,...

This Nintendo Switch Cyber Monday deal gives you more storage and 12 months online

The Nintendo Switch is a fantastic system, but with only 32GB of internal storage available, space can run out fast. You also need...

Sage Business Cloud Accounting review

This is a review of Sage Business Cloud Accounting for North America. The product, and its features may vary depending on the country...
- Advertisement -