Bug Pattern In Java - Null Pointers Everywhere - Online Article

This article is a quick interlude to expose you to null pointers and the NullPointerException, a frequently occurring cause of several bug patterns.

One of the most common (and most commonly complained about) symptoms of bugs in Java programming is a thrown NullPointerException. Tracking down the cause of such exceptions can truly make you question your career decision. They provide no useful information as to why they were thrown and they are difficult-perhaps impossible in many cases-to predict.

They're Uninformative

Of all the exceptions a Java programmer might encounter, the NullPointerException is among the most dreaded, and for good reason: it is one of the least informative exceptions that a program can signal.

Unlike, for example, a ClassCastException, a NullPointerException says nothing about what was expected instead of the null pointer. Furthermore, it says nothing about where in the code the null pointer was actually assigned.

In many NullPointerExceptions, the true bug occurs where the variable is actually assigned the null value. To find the bug, we have to trace back through the flow of control to discover where the variable was assigned and determine whether doing so was incorrect. This process can be particularly frustrating when the assignment occurs in a package other than the one in which the error was signaled.

They're Elusive

Many Java developers note that the vast majority of program crashes they encounter are NullPointerExceptions; they long for a tool that would statically identify these bugs before the program is run. Unfortunately, automata theory- the study of machines, robots, or systems which follow a preset sequence of instructions automatically-tells us that no tool could ever statically determine exactly which programs will throw NullPointerExceptions.

But it is possible for a tool to rule out NullPointerExceptions for many expressions in a program, leaving us with just a few small potential trouble spots that we must then examine manually. In fact, several research efforts are now underway to provide just such a tool for Java programs. But a good tool can only take us so far.


Automata theory says that no tool can ever statically determine exactly which programs will throw NullPointerExceptions.

NullPointerExceptions will never be completely eradicated. When they do occur, it helps us to know the bug patterns associated with them so that we can diagnose them quickly. In addition, we can use certain programming and design techniques to significantly minimize the occurrence of these patterns.

Now, on to two bug patterns that list null pointers as their cause: the Dangling Composite and the Null Flag.

About the Author:

No further information.


No comment yet. Be the first to post a comment.