File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is it NullPointerException?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is it NullPointerException??" Watch "Is it NullPointerException??" New topic
Author

Is it NullPointerException??

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435

Options :
A: The code does not compile
B: The code compiles, it will give NullPointException at runtime.
C: The code compiles and runs with output: null null.
D: The code compiles, it will give ClassCastException at runtime.

Answer : c)
I feel the answer should be b) and will give a Runtime exception when run
please explain me
Sonir
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Sonir :
Why do you think it should give null pointer exception? At which line?
When you pass an object to the println or print methods what is the normal expected behavior? What does the API say about the method?
What happens in this case that make you think it will create a nullpointer exception? What does the API say about nullpointer exceptions- when are they supposed to occur?

Once you see an answer to a question, you should compile it and run it yourself and try to break down the code logically. Look at each line of code and decipher what is happening. Look up the API documentation on the objects/methods in question.

In the real exam, you can not post on JavaRanch and get the answers. You can't even compile and run the code. You have to be able to read the code and figure out what's going to happen.
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Sonir:
I do not mean to be discouraging. Please take my post above in the right spirit. I know you are taking the exam next week and I am worried for you. That's all.
HTH
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi,
NullPointerException is thrown at runtime when we try to access a reference by calling a method or accessing it's variable and the ref is null.
here, when we cast 't' to T object at runtime it works as we still don't try to access the ref 't'. so no exception is thrown.
regards
maulin
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
I think the key point this example is showing is that null is a valid reference value. That is, you can assign null to an object reference variable in the same way you assign an object reference.
Object obj = new Object()
and
Object obj = null
Are both valid assignements. Additionally, casting a reference that contains a null value is allowed as long as the types of the variables allow the cast; The System.out.println() method also deals ok with null values, printing "null" .
The only time you will actually get a null pointer exception is if you try to call a method or access an instance member from a reference variable that has a value of null.
For example,
String str = "hi";
System.out.println(str.toString()); //this prints "hi"
String str2 = null;
System.out.println(str2.toString()); //this will give a null pointer exception.

Rob


Rob
SCJP 1.4
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
the cast is not needed here.
it is automatically given or implicitly given by the compiler, so you don't have to cast.
right ??
Originally posted by Maulin, Vasavada:
hi,
NullPointerException is thrown at runtime when we try to access a reference by calling a method or accessing it's variable and the ref is null.
here, when we cast 't' to T object at runtime it works as we still don't try to access the ref 't'. so no exception is thrown.
regards
maulin
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Actually everything you feed into System.out.println has to be converted to a String. For objects, the toString method is invoked upon them and primitive are first converted into their objects counterpart (Integer for int, Double for double, etc...). So sonir was probably wondering why "null" is printed out when giving a null reference argument since the toString method has to be invoked upon the reference.. That's a good question Sonir.
So I have a good explanation for this (I guess)...
System.out is of type PrintStream. Its method println(Object obj) invokes print(Object obj) and the latter invokes String.valueOf(obj).
Now in class String the method valueOf(Object obj) has the following implementation:

So if the reference to the object is null, then the String "null" is printed otherwise the toString() method is invoked on obj...
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Excellent explanation Valentin. I was hoping to led him to that explanation on his own with my questions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it NullPointerException??