• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Null Pointer Exception in Java

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic