views:

13892

answers:

5

What are null pointer exceptions and what causes them in general?

+23  A: 

They're exceptions that occur when you try to use a reference that points to no location in memory (null) as though it were referencing an object. Calling a method on a null reference or trying to access a field of a null reference will trigger a NPE. These are the most common, but other ways are listed on the NullPointerException javadoc page.

Probably the quickest example code I could come up with to illustrate a NPE would be:

public class Example
{
    public static void main(String[] args)
    {
        Object obj = null;
        obj.hashCode();
    }
}

On the first line inside main I'm explicitly setting the Object reference obj to null. This means I have a reference, but it isn't pointing to any object. After that, I try to treat the reference as though it points to an object by calling a method on it. This results in a NPE because there is no code to execute in the location that the reference is pointing.

(This is a technicality, but I think it bears mentioning: A reference that points to null isn't the same as a C pointer that points to an invalid memory location. A null pointer is literally not pointing anywhere, which is subtly different than pointing to a location that happens to be invalid.)

Bill the Lizard
I understood everything you wrote there, but only because I've been coding for a while and know what a 'pointer' and a 'reference' are (and what null is, for that matter). When I try to dive right into explanations like that, my students look at me crosseyed, because there's not enough background.
mmr
@mmr: Thanks for the feedback, you make a valid point. It's difficult on the internet to really judge where someone is at, and at what level it's safe to start an explanation. I'll try revising this again.
Bill the Lizard
+6  A: 

A null pointer exception is caused when you dereference a variable that is pointing to null. See the following code:

String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
smink
+4  A: 

A NULL pointer is one that points to nowhere. When you dereference a pointer "p", you say "give me the data at the location stored in "p". When p is a null pointer, the location stored in "p" is "nowhere", you're saying "give me the data at the location 'nowhere'". Obviously it can't do this, so it throws a NULL pointer exception.

In general, it's because something hasn't been initialized properly.

MrZebra
+21  A: 

When you declare a reference variable (i.e. an object) you are really creating a pointer to an object. Consider the following code where you declare a variable of primitive type int:

int x;
x = 10;

In this example the variable x is an integer and Java will initialize it to 0 for you. When you assign it to 10 in the second line your value 10 is written into the memory location pointed to by x.

But, when you try to declare a reference type something different happens. Take the following code:

Integer num;
num = new Integer(10);

The first line declare a variable named num, but, it does not contain a primitive value. Instead it contains a pointer (because the type is Integer which is a reference type). Since you did not say as yet what to point to Java sets it to null, meaning "I am pointing at nothing".

In the second line the new keyword is used to instantiate (or create) an object of type Integer and the pointer variable num is assigned this object. You can now reference the object using the dereferencing operator . (a dot).

The Exception that you asked about occurs when you declare a variable but did not create an object. If you attempt to dereference num BEFORE creating the object you get a NullPointerException. In the most trivial cases the compiler will catch the problem and let you know that "num may not have been initialized" but sometime you write code that does not directly create the object.

For instance you may have a method as follows:

public void doSomething(Integer num){
   //do something to num
}

in which case you are not creating the object num, rather assuming that is was created before the doSomething method was called. Unfortunately it is possible to call the method like this:

doSomething(null);

In which case num is null. The best way to avoid this type of exception is to always check for null when you did not create the object yourself. So doSomething should be re-written as:

public void doSomething(Integer num){
    if(num != null){
       //do something to num
    }
}
Vincent Ramdhanie
Wow, very thorough! I figured out what I did wrong:GLabel string = new GLabel("string", WIDTH / 2 - string.getWidth() / 2, HEIGHT / 2 + string.getAscent() / 2)I was dereferencing(?) string while I was declaring it. I broke this up into two lines and that worked.Thanks!
Ziggy
"Since you did not say as yet what to point to Java sets it to null, meaning "I am pointing at nothing"."This is wrong! Java only does this with field declarations. If you declare a local variable in a method but do not initialize it, Java does not "set it" to anything.
oxbow_lakes
Chris, I guess my choice of words may cause unfortunate ambiguity. I was hoping to imply that its null because you did not set it. But thanks for the correction.
Vincent Ramdhanie
A: 

In java all the variables you declare are actually "references" to the objects ( or primitives ) and not the objects them selves.

When you attempt to execute one object method, the reference ask to the living object to execute that method. But if the reference is referencing NULL ( nothing, zero, void, nada ) then there is no way the method get's executed. Then the runtime let you know this by throwing a NullPointerException.

Your reference is "pointing" to null thus "Null -> Pointer"

The object lives in the VM memory space and the only way to access it if using this references. Take this example:

public class Some {
    private int id;
    public int getId(){ 
        return this.id;
    }
    public setId( int newId ) { 
        this.id = newId;
    }
}
....
....
// Somewhere else...
Some reference = new Some();    // point to a new object of type Some()
Some otherReference = null;     // initiallly this points to NULL

reference.setId( 1 );           // execute setId method, now private var id is 1

System.out.println( reference.getId() ); // prints 1 to the console

otherReference = reference      // Now they both point to the only object.

reference = null;               // "reference" now point to null.

// But "otherReference" still point to the "real" object so this print 1 too...
System.out.println( otherReference.getId() );

// Guess what will happen
System.out.println( reference.getId() ); // :S Throws NullPointerException because "reference" is pointing to NULL remember...

This an important thing to know, when there is no more references to an object ( in the example above when "reference" and "otherReference" point to null ) then the object is "unreachable" there is no way we can work with it, so this object is marked for to be garbage collected, and at some point the VM will free the memory used by this object, and will allocate another.

OscarRyz