This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
: 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).]
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.
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
posted
0
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.
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.)
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.
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