Preventing NullPointerException

From Wikibooks, open books for an open world
Jump to: navigation, search

Unchecked Exceptions Java Programming
Preventing NullPointerException
Nesting Exceptions
Navigate Exceptions topic:v  d  e )

This page describes some techniques for preventing NullPointerException.

It does not describe general techniques for how you should program Java. It is of some use, to make you more aware of null values, and to be more careful about generating them yourself.

Note that this list is not complete - there are no rules for preventing NullPointerException entirely in Java, because the standard libraries have to be used, and they can cause NullPointerExceptions. Also, it is possible to observe an uninitialised final field in Java, so you can't even treat a final field as being completely trusted during the object's creation.

A good approach is to learn how to deal with NullPointerExceptions first, and become competent with that. These suggestions will help you to cause less NullPointerExceptions, but they don't replace the need to know about NullPointerExceptions.

[edit] Comparing string variable with a string literal

When you compare a variable with a string literal, always put the string literal first. For example do:

Computer code
if ("OK".equals(state) )
{
  ...
}

and do not do:

Computer code
if (state.equals("OK") )
{
  ...
}

If the 'state' variable is null, you get a NullPointerException in the second example, but not in the first one.

[edit] Minimize the use of the keyword 'null' in assignment statements

This means not doing things like:


Computer code
String s = null;
while (something) {
    if (something2) {
        s = "yep";
    }
}

if (s != null) {
    something3(s);
}


You can replace this with:


Computer code
boolean done = false;

while (!done && something) {
    if (something2) {
       done = true;
       something3("yep");
    }
}


You might also consider replacing null with "" in the first example, but default values bring about bugs caused by default values being left in place. A NullPointerException is actually better, as it allows the runtime to tell you about the bug, rather than just continue with a default value.

[edit] Minimize the use of the new Type[int] syntax for creating arrays of objects

An array created using new Object[10] has 10 null pointers. That's 10 more than we want, so use collections instead, or explicitly fill the array at initialisation with:


Computer code
Object[] objects = {"blah", 5, new File("/usr/bin")};


or:


Computer code
Object[] objects;
objects = new Object[]{"blah", 5, new File("/usr/bin")};


[edit] Check all references obtained from 'untrusted' methods

Many methods that can return a reference can return a null reference. Make sure you check these. For example:


Computer code
File file = new File("/etc");
File[] files = file.listFiles();
if (files != null)
{
    stuff
}


File.listFiles() can return null if "/etc" is not a directory.

You can decide to trust some methods not to return null, if you like, but that's an assumption you're making. Some methods that don't specify that they might return null, actually do, instead of throwing an exception.


Unchecked Exceptions Java Programming
Preventing NullPointerException
Nesting Exceptions
Personal tools
Namespaces

Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export