File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException doubt

 
meera kanekal
Ranch Hand
Posts: 75
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic