Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it NullPointerException??

 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent explanation Valentin. I was hoping to led him to that explanation on his own with my questions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic