Detailed Explanation in API document
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/NullPointerException.html
In general, you would like to do something for an Object, but it is null
e.g. You would like to roll a dice, but the dice Object is null.
You have to initialise your variable to an instance of Object before use.
Dice yourDice;
int number = 0;
number = yourDice.roll(); // NullPointerException
yourDice = new Dice();
number = yourDice.roll(); // OK
It means exactly what the docs say it means.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/NullPointerException.html
(Note that there's no trailing "s".)
For example:
Foo foo = null;
foo.doSomething(); // can't dereference null. can't invoke a method on null
> Detailed Explanation in API document
> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Null
> PointerException.html
>
> In general, you would like to do something for an
> Object, but it is null
> e.g. You would like to roll a dice, but the dice
> Object is null.
> You have to initialise your variable to an instance
> of Object before use.
>
> Dice yourDice;
> int number = 0;
> number = yourDice.roll(); // NullPointerException
> yourDice = new Dice();
> number = yourDice.roll(); // OK
Just to be a nitpicker...
The above code won't compile. Assuming that's inside a method, yourDice is a local variable, and therefore has no default value. It's not null--it's undefined. You'll get "yourDice may not have been intialized" ad the yourDice.roll() line when you try to compile. You'd need to add = null to the yourDice declaration.
If, on the other hand, yourDice is a member variable, then the statements number = ... etc. are not legal outside a method.