• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

srange null..!

 
Ernest Lee
Ranch Hand
Posts: 145
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test001 {
int i;
public Test001(int i) {
this.i = i;
}
public String toString() {
if(i == 0) return null; else return "" + i;
}
public static void main(String[ ] args) {
Test001 t1 = new Test001(0);
Test001 t2 = new Test001(2);
System.out.println(t1); //1
System.out.println(t2); //2
}
}
Why //1 will throws Exception as is while will print "null" if t1 is replaced with t1.toString()?? Isn't it true that the println will call toString method for t1 implicitly anyways???
can someone explain?
 
Usha Damarla
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernst,
You got this question from Guoqiao Sun's mock exam site, right.
Infact the toString() method gets called here. Guoqiao
actually forgot to concatenate "null" with an empty string. The original stmt
if(i == 0) return null;
was actually returning a null value instead of a string, causing it to fail at runtime.
Change the code like this and try again.
public String toString()
{
if(i == 0) return ""+null;
else return "" + i;
}

Guoqiao, its ur wish to modify either question or your explanation.

Regards
Usha
ps: Removed the < PRE > tags in the post
since it was difficult to read.
- satya

[This message has been edited by Madhav Lakkapragada (edited December 18, 2001).]
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Helo!
Deeper explanation may be found on Sun sources?!
But yet the solution presented by Usha works alright as well as printing object like this:
System.out.println( "" + t1 );
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java� 2 Platform
 
Dave Vick
Ranch Hand
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest
Check out this thread here
It looks like the same question.

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This program shows the difference

This the output from javap for the line //1
15 invokevirtual #6 (Method void println(java.lang.String))
and this for the other
22 invokevirtual #7 (Method void println(java.lang.Object))
println(String) was written for returning "null" whenever the argument was null.
But println(Object) was not written for providing a "null" String to the write method when the method toString() of the Object argument returned null.
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Excellent, Jose... I am in the clear now
-Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 05, 2001).]
 
marilyn murphy
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Jose. That clarifies the answer for me.
 
Nguyen Trung
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jose,
Please give more explanation to a novice.still don't understand what you said about println method.
System.out.println((Object)null) ;
Why does the above stmt give null as output ?
thanx
 
Cameron Park
Ranch Hand
Posts: 371
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically Jose was saying the two overloading methods
println(String) and println(Object)
println(String) will send null to the writer if the argument is null.
On the other hand, println(Object) will not send null to the writer when the Object's toString() returns null.
HIH.
 
MONZY THARIAN
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jose.The explanation though simple was very tricky
 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quote:
--------------------------------------------------------------------------------
Originally posted by Jose Botella:
But println(Object) was not written for providing a "null" String to the write method when the method toString() of the Object argument returned null.
--------------------------------------------------------------------------------

Hi
I am pasting source code for println(Object obj):
from System.java :
public final static PrintStream out = nullPrintStream();
--------------------------------------------------------------
from PrintStream.java :
public void println(Object x) {
synchronized (this) {
print(x);
newLine();
}
public void print(Object obj) {
write(String.valueOf(obj));
}
-----------------------------------------------------------------
from String.java :
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}

Now will any one please tell me why NullPointerException should be thrown?
Thanks in adv

------------------
Regards
Ravish
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now will any one please tell me why NullPointerException should be thrown?

NullPointerException is a runtime exception and hence doesn't have to be part of the method declaration, if that's what you are trying to point in the code.
regds.
- satya
 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Madhav Lakkapragada:

if that's what you are trying to point in the code.
regds.
- satya

No, I am trying to say that :
if Object type refernce is null still String "null" should be returned.(in case of System.out.println(someObjRefWhichIsEqualToNull))
if you follow the method calls, you will find no where null.anyMethod() has invoked. Every time you are suppose to get "null" String(in case of null) but still we get NullPointerException.
TIA

------------------
Regards
Ravish
 
sun ram
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stack trace of this program.
java.lang.NullPointerException
at java.io.Writer.write(Writer.java:129)
at java.io.PrintStream.write(PrintStream.java:267)
at java.io.PrintStream.print(PrintStream.java:426)
at java.io.PrintStream.println(PrintStream.java:563)
at Test001.main(Test001.java:15)
See Control goes to java.io.Writer.write method. In write method str.length() call throws the Nullpointer Exception.
/**
* Write a string.
*
* @param str String to be written
*
* @exception IOException If an I/O error occurs
*/
public void write(String str) throws IOException {
write(str, 0, str.length());
}
 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why should write(String str) throw an exception.
as
>> at java.io.PrintStream.print(PrintStream.java:426)
public void print(Object obj) {
write(String.valueOf(obj));
}
will call String.valueOf(obj) which is in turn is:
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
So if obj is null then it will return "null".
NOW when write(String str) is called:
public void write(String str) throws IOException {
write(str, 0, str.length());
}
str.length() should return 4 not NullPointerException.
I think I have cleared my doubt now ..

------------------
Regards
Ravish
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, I see your point.....
Here is how it all goes.
The principal difference is where the obj.toString()
gets called?
.



Case 1:
Line 1 executes like this.........

System.out.println(t1.toString());// Line 1
Since the class over-rides the toString()
method, the call becomes:
System.out.println(null); //null - Object reference
Following the code all the way to String.java

The String literal "null" is returned (because our pbject is
null and successfully printed.
Line 2 explanation goes like this.........
On the other hand,
System.out.println(t1);//NullPointerException Line 2
Here the obj reference is t1 and is NOT null.
Hence when you trace the code all the to String.java,
the if stmt decides to to call the toString() method. Since
the class of the object (Test001) over-rides this method
and return null as a String object reference, we get the
NullPointerException.
However, if you want the behavior you are expecting, try this code......

NOw observe that the toString() method of the Test001 class
returns a String with the literal "null" and NOT a null object reference. Thus you will avoid the NullPointerException.
HTH.
- satya

Edited:
ps: While posting this msg, I missed your earlier post and "sun ram" post above. If your doubt is cleared, feel free to ignore my msg.
regds.
- satya

[This message has been edited by Madhav Lakkapragada (edited December 18, 2001).]
 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thansk a lot ..
got my error too..
t1 is not null.... toString() returns null..
so to call to write() will throw NPExcep....

Thanks a lot to all of you .....

------------------
Regards
Ravish
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic