Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Null Object

Reply
Thread Tools

Null Object

 
 
Moon2
Guest
Posts: n/a
 
      03-20-2007
Can anyone please tell me why the following doesnt create a runtime error.

Object s = null;
System.out.println(s);

The output is null. Why doesnt it call the Object's tostring method and
throw a null pointer exception. Any comments will be appreciated.

Moon2



 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      03-20-2007
"Moon2" <> writes (with minor modifications):
>Why doesnt it call the object's tostring method and
>throws a null pointer exception?


This is the result of a a design decisions.

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      03-20-2007
(Stefan Ram) writes:
>This is the result of a a design decisions.


I meant »This is the result of a design decision.«.

Usually, when null is not a valid value, one wants a receiver
of the value to mark failure as early as possible, e.g., by
throwing.

But it makes sense for some receivers to accept null as a
possible value. This depends on the context.

In the case of »println«, it is often more helpful to show the
null than to abort the action, e.g., when used for debugging.

 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      03-20-2007

"Moon2" <> wrote in message
news:etpgtd$rjt$...
> Can anyone please tell me why the following doesnt create a runtime error.
>
> Object s = null;
> System.out.println(s);
>
> The output is null. Why doesnt it call the Object's tostring method and
> throw a null pointer exception. Any comments will be appreciated.


Because the engineers who defined the method deemed that to be less useful
behavior that what it actually does. Note that this isn't a general
language issue, it's specific to OutputStream.println(Object ). If you'd
asked about

Object s = null;
String msg = "The answer is " + s;

Now it's a language issue, though the answer remains pretty much the same.


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      03-20-2007

"Moon2" <> wrote in message
news:etpgtd$rjt$...
> Can anyone please tell me why the following doesnt create a runtime
> error.
>
> Object s = null;
> System.out.println(s);
>
> The output is null. Why doesnt it call the Object's tostring method and
> throw a null pointer exception. Any comments will be appreciated.


System.out.println() doesn't actually directly call the passed-in
object's toString() method. Instead, it calls String.valueOf(s). The
implementation of the String.valoeOf(Object) method is:

public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}

That is why you see the output "null" instead of an NPE.

- Oliver


 
Reply With Quote
 
Moon2
Guest
Posts: n/a
 
      03-20-2007
Thanks very much for clearing things up

Moon2


 
Reply With Quote
 
Hendrik Maryns
Guest
Posts: n/a
 
      03-21-2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Moon2 schreef:
> Thanks very much for clearing things up


By the way, note that your subject is misleading: this is not about a
Null Object (there is non in the standard Java API), but about the null
*reference*.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFGAQFge+7xMGD3itQRAjltAJwJ5ADS9SkcSySBObKW2x XGJ7QzIQCfdDBY
xXRZK9uLUoMHI05HWIA5pKc=
=HspG
-----END PGP SIGNATURE-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASPX page jscript rt error: null is null or not an object Cirene ASP .Net 1 06-09-2008 07:59 PM
XMLHTTP - null is null or not an object Tim Platt Javascript 0 06-04-2007 08:42 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
difference between null object and null string gokul.b@gmail.com Java 16 10-12-2005 06:43 PM
IE SP2 solution for "null is null or not an object" and "broken" insertCell/insertRow putty Javascript 1 04-05-2005 07:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57