NullPointerException in Java: Causes and Ways to Avoid It

NullPointerException: the Most Common Exception

NullPointerException (NPE) is the most common exception in Java. The cause of this exception is known but looks like that in most cases developers prefer to ignore it and don't take any action. I personally expect that reasons for such behavior are the following: 

  • Most of the developers don't see any problem here and recognize all NPE exceptions as the developer's fault. 
  • Developers who are aware of this design problem don't know how to fix it. 

In this article, I'll explain what is the root of this problem and provide ways to fix that problem.

A Look at Java Optionals

Optional is a container object that may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value.

Package : java.util
public final class Optional<T>
extendsObject


A Deep Dive Into Java Debugging

This is the story of a Java debugging journey that started with a question I couldn't answer about Java stack traces. As a long-time Java programmer, I am approached for help by developers who encounter unusual problems in the language. Diving in and getting acquainted with all the dark corners of Java is something I really enjoy, mainly because I emerge with a better understanding of the language, and also because it equips our team with better tools to solve the everyday problems ... as well as the unusual ones.

Our trip takes us through a deeper look at Java lambdas and method references and ends up with a quick foray into the JVM code. We'll use a couple of debugging tools and techniques to figure it all out and learn a little about implementation details and diagnostic JVM options. It's a good example of how, with the source in hand, you can demystify a general phenomenon, such as missing frames.