Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Unable to detect null behaviour

 
Raghuram Nanda
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Robert Young
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic