• 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

Printing an object with null reference

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
When the following code is executed:
public class Test{
static String s1;

public static void main (String args [])
{
System.out.println(s1);

}
}
null gets printed instead of throwing a NullPointerException.
Can someone please explain why?
Thanks
Porchelvi
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is only the case of String objects. If a reference to a String object is null, then printing it will print the String "null". BUT if you have a reference to any other kind of objects, then a NullPointerException will be thrown, because the toString method will be invoked on a null reference...
Anyone correct me if i am wrong ..
Val
 
Porchelvi Vendhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Thanks for the reply.But I tried printing an Integer object
For example,
public class Test{
static Integer i1;
public static void main (String args [])
{
System.out.println(i1);

}
}
This prints null.
I am really puzzled.
Porchelvi
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you did not initialize your Integer object, when your class is initialized i1 will be initialized to null !!
Try initializing it to something valid !!
Val
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Porchelvi,
The class variable 's1' is initialized to the default value for objects: a 'null' object.
When you pass an object to the System.out.println method the object's toString() method is invoked. The 'null' object inherits Object.toString() the same as every other object. Invoking toString() on a null does not raise an exception.
If you try using a 'null' object for other operations you will get a NullPointerException. For example, try adding the <code>s1.trim()</code> to your code.
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Porchelvi Vendhan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Thanks for the reply.
But,
public class Test{
static Integer i1;
public static void main (String args [])
{
System.out.println(i1.toString());

}
}
now NullPointerException is thrown

Porchelvi
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you cannot call a method on a null since null is not an object.
 
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic