Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes NullPointerException doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NullPointerException doubt" Watch "NullPointerException doubt" New topic
Author

NullPointerException doubt

meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
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

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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException doubt
 
Similar Threads
Java NullPointer Exception
instanceof operator with null
NullPointerException
valueOf vs. parseInt?
operator