aspose file tools*
The moose likes Beginning Java and the fly likes Null Pointer Exception in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Null Pointer Exception in Java" Watch "Null Pointer Exception in Java" New topic
Author

Null Pointer Exception in Java

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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

Joined: Jul 10, 2001
Posts: 234
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

Joined: May 08, 2001
Posts: 1932
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

Joined: Jul 10, 2001
Posts: 234
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

Joined: Feb 22, 2001
Posts: 399
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

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5370
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


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null Pointer Exception in Java
 
Similar Threads
Unable to detect null behaviour
a highly recommended mock site: http://www.jiris.com/mock
Mock question
Jiris Mock 1, Q1
Does the toString() method cannot return null?