http://java.sun.com/j2se/1.5.0/docs/api/java/lang/NullPointerException.html In the above link to sun's documentation on NullPointerlException could you please explain what the following means with an example?
1) Taking the length of null as if it were an array.
Does this mean trying to get the length of an array which is null?
2) Accessing or modifying the slots of null as if it were an array.
Does this mean trying to modify an array which is null?
3) Throwing null as if it were a Throwable value.
Does this mean throwing an exception on a value that is null?
Thanks,
Meera
Ken Truitt
Ranch Hand
Joined: Aug 23, 2007
Posts: 124
posted
0
Does this mean trying to get the length of an array which is null?
...
Does this mean trying to modify an array which is null?
...
Does this mean throwing an exception on a value that is null?
Yes I think you're right. Any time you have a variable that points to null, and
you attempt any of those operations on it, the NullPointerException will be
thrown.
SCJP 88% | SCWCD 84%
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
0
That's correct, except that if you just throw the literal value null it will compile (in other words, you don't need to use a Throwable reference variable,) but fail with a NPE at runtime. However, trying to get the length attribute or an indexed element via the null literal will give you a compile error.
All code in my posts, unless a source is explicitly mentioned, is my own.