• 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

NullPointerException

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Here I am again... Can you help me with something here ?
In the following code :


I was expecting line l to throw a NullPointerException. How come it prints null ?
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Giselle,

I was expecting line l to throw a NullPointerException.


NullPointerException would have been thrown had one tried to access any method through the object reference a, which is not initialized.
e.g. a.toString() //Error - NullPointerException!
The declaration A a sets the reference variable a to null (just as int i would set i to zero). It is then printed out in System.out.println()
 
Giselle Dazzi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but isnt the method toString() called ?
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Giselle. Let me see if I'm understanding my SCJP studies ;-).
a is an instance variable, so it'll be initialized by null.
since you're doing :

you're concatenating a string representation with the value of the instance variable, which is null (not pointing to anything), so the null value gets promoted to a String (the toString method is not invoked). So it prints:
a = null
please correct me if I'm wrong.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is an excerpt from JLS(sect. 15.18.1.1) regarding String concatenation:

"If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l). Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments..."
 
Giselle Dazzi
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Alton, that�s the info I was looking for. I thought toString() was called no matter what...
Andres, you are right. I was questioning the fact that toString is part of an object not yet created, so how could it be called ?
[ June 03, 2003: Message edited by: Giselle Dazzi ]
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was a little confused about that myself, because you can pass a null reference to System.out.println and it's happy, yet if you try this, you get NullPointerException.
String a=null;
StringBuffer sbuffer = new StringBuffer(a);
I guess in this case, toString() is being called by StringBuffer. Although:
sbuffer.append(a); // Fine, prints "null".
Forget about using null literals:
sbuffer.append(null); // No good, because of overloaded methods, so you get a compile error of "ambiguous".
Hope this helps more than straying from the topic at hand.
[ June 03, 2003: Message edited by: Brian Joseph ]
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic