Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException doubt

 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic