RUN TIME ERROR

java.lang.NullPointerExceptionswhat does that mean exactly?
[73 byte] By [thehurricanea] at [2007-11-27 3:31:27]
# 1

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

rym82a at 2007-7-12 8:34:28 > top of Java-index,Java Essentials,New To Java...
# 2

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

jverda at 2007-7-12 8:34:28 > top of Java-index,Java Essentials,New To Java...
# 3

> 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.

jverda at 2007-7-12 8:34:28 > top of Java-index,Java Essentials,New To Java...
# 4
o...missed that :(thanks for correction.
rym82a at 2007-7-12 8:34:28 > top of Java-index,Java Essentials,New To Java...
# 5
That helped alot thanks
thehurricanea at 2007-7-12 8:34:28 > top of Java-index,Java Essentials,New To Java...