• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null Pointer Exception in Java

 
Thomas Markl
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this code throw a Nullpointer Exception?
Source: http://www.geocities.com/sun_guoqiao/scjp/mockexam1.html

: It will throw the following exception at runtime at //1.
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:18)
But it is strange that
System.out.println(t1.toString());
will run properly with output null.
Thomas

[This message has been edited by Cindy Glass (edited December 18, 2001).]
 
BJ Grau
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are passing null to println(Object), so somewhere along the chain of method calls, a method is being called on null, which throws the NullPointerException.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error seems to deal with the length() method in String. When Writer.write(String) is called, it calls an overloaded write(str, 0, str.length()); method. If you write test code like this:

You'll get the same type of error. Apparently, null Strings can not have a set length, so when a method calls for s.length(), you get this exception.
Jason
[This message has been edited by jason adam (edited September 04, 2001).]
 
BJ Grau
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right. Also don't forget the part where you can't call a method on null. Thats what your doing when you try s.length() when String s = null.
 
John Dale
Ranch Hand
Posts: 399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to Java Class Libraries, Second Edition, Volume 1, toString() must return a non-null string. (page 1224) Perhaps the compiler is generating code that calls toString(), knowing that toString cannot return null. (Checking that out would take a bit more work than I'm willing to do tonight.)
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey you are right.
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.
 
R K Singh
Ranch Hand
Posts: 5382
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic