• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to detect null behaviour  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code
System.out.println(t1); //throws null pointer exception
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);//throws null pointer exception
System.out.println(t2);
}
}

But in this code it doenpt throw?
public class Demo
{
static String s;
public static void main(String args[])
{
System.out.println(s);//Here nullpointer exception not thrown
}
}
Can anyone explain in detail abt these two indifferent behaviours....
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what is happening is that when you are calling the System.out.println() function it is automatically calling the .toString() function of that class. In your toString() function you are telling it to return a null on a zero value. You are getting the null pointer when you are initializing the object with a zero value, thus the null on the print.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raghuram
I am not quite sure about this but I guess what is happening here is when you call System.out.println(t2); the value returned is "2". So in System.out.println("2".toString()); might be taking place. But when you do System.out.println(t1); it is equivalent to System.out.println(null.toString()); and because this happens at runtime it gives you an exception.
I haven't had a look at the toString() method of the String class. But probably it checks if the object is null and in that case returns "null".
So System.out.println(s); gives null.
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a try:
public void print(Object obj),from API

Print an object. The string produced by the String.valueOf(Object) method is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.


System.out.println(Object o); --> System.out.println(String.valueOf(o));
And for String.valueOf(Object o), again from API

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.


And now lets try the problem:
1.
String s=null;
System.out.println(s);
here s==null, so the string "null" will be returned by the String.valueOf() method and get translated and printed by the print() method.
2.
System.out.println(t1.toString());
here t1.toString() will be evaluated first and null, of String type(since the return type of toString method is String), is returned and the expression is actually the same as that in case 1, thus "null" is printed without any exceptions.
3.
System.out.println(t1);
here t1 is not null, and thus String.valueOf(t1), t1.toString() is called and the return string is said to be translated into bytes according to the platform's default character encoding.....err....nullpointer exception arises when attempting to translate.....(but i dun know what's the translatation process is, that's why i put "just a try" at the very beginning)
This is probably the only case that a System.out.println/print () could throw a null pointer exception, the case that a statement tries to print a nonnull object for which a null string is returned by its toString() method.
Could anyone explain more on this issue?
[ June 09, 2003: Message edited by: Yi Meng ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

java.lang.NullPointerException
at java.io.Writer.write(Writer.java:126)
at java.io.PrintStream.write(PrintStream.java:303)
at java.io.PrintStream.print(PrintStream.java:462)
at C.main(Test42.java:11)

NullPointerException occurs at str.length() because str is null.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!