File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NullPointerException " Watch "NullPointerException " New topic


Giselle Dazzi
Ranch Hand

Joined: Apr 20, 2003
Posts: 168
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 ?

Giselle Dazzi<br />SCJP 1.4
Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
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

Joined: Apr 20, 2003
Posts: 168
but isnt the method toString() called ?
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
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.

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
The following is an excerpt from JLS(sect. 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

Joined: Apr 20, 2003
Posts: 168
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 ]
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
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 ]
I agree. Here's the link:
subject: NullPointerException
It's not a secret anymore!