“Vaccine” to prevent NullPointerException in Java

Tram Ho

After a while working with Java, I have accumulated quite a bit of experience when I encountered NullPointerException, this is an extremely subtle error (family RuntimeException – occurs only when the program is running) and not always it also happens. So in this article, I will share about NullPointerException and some tips to avoid this error before the program crashes.

1. What is NullPointerException?

In simple words, NullPointerException is an Exception that occurs when you call actions on a reference variable that is not pointing to any object. This problem is extremely common in Java because except for primitive variables, all other objects are reference variables. And as I wrote above, it doesn’t always happen. out, let’s see the following code:

With luck (and it’s up to your code to decide), studentNames will return a non-empty list and the above code is perfectly fine. But… What if studentNames has null value? Your program crashes immediately, the reason we call the method size while the list is running null.

Note: some IDEs have a pretty good reminder mechanism like IntelliJ that will put the big one right away to enlighten you to see the Exception before it’s too late. But as I stated, it doesn’t always happen and the light bulb also appears.

Okay, I made an example that seems easy to detect, but if you are a Java dev, you must have tasted 1000 times more powerful variants like this. Next we will go through some “Vaccines” that we use to prevent this Exception.

2. Vaccine #1: Try Catch (TC)

Not only NullPointerException, TC is an effective vaccine for every RuntimeException in Java, TC helps you set a simple “trap” to catch Exceptions and make sure this trap is always successful, I will inject TC for the code for example. for example as follows

The trade-off of simplicity will be that your code is more cumbersome and complicates the problem in cases where the occurrence of this Exception is not so important, for example when studentNames to be null then just omit the line of calling code size. However, TC is still very often used to make logging and bug tracking easier.

3. Vaccine #2: If Else (IE)

This vaccine I think is the most popular in pet projects, because it is much simpler than TC but extremely effective in cases where it doesn’t matter. Exception

Is anything wrong? IE deserves to be every developer’s vaccine

4. Vaccine #3: Optional

This type was added in Java 8, I came across it when I was working with Spring Framework, specifically with JPA’s methods to get an object (findBy…) that were all caught in Optional.

With Optional, we have shorter syntax and… and compact code. Actually, when you learn about Optional, you’ll see that there are many other great methods to focus on handling Maybe types (the kind that has to be if else).

Why don’t I continue with the examples in the previous two vaccines? Basically Optional is not designed to be so cumbersome. I can get it size not null without using 2 vaccines first by:

Extremely useful Util packages like StringUtils, CollectionUtils, ListUtils will help us do less work, so please take advantage of it (most of them have a perfect null handling mechanism !!!).

5. Summary

So I went through some vaccines (completely FREE) to help devs prevent this annoying NullPointerException bug, if you have crafted/found any other vaccines or “antibodies”, please share. everyone.

Happy coding!

My original post: https://phatng.com/vaccine-ngua-nullpointerexception-trong-java

6. References

  1. Almost refer to my experience. My blog: phatng.com
  2. Guide To Java 8 Optional – https://www.baeldung.com/java-optional
Share the news now