This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A println() peculiarity... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A println() peculiarity..." Watch "A println() peculiarity..." New topic

A println() peculiarity...

Matthew Alesi
Ranch Hand

Joined: Sep 13, 2006
Posts: 38
Came across this problem writing a program for my Computer Science class. Reproduced here with simpler code:

All but the last call in main() work fine; the last throws a null pointer exception. What is the explanation for this seeming inconsistency?

Current CS undergrad
SCJP 5.0
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3636
You are effectively passing 'null' in the first 3 calls. In the last call you are passing an object, whose toString() is returning a null.
The println method checks if the passed object is null (not whether the passed object's toString() returns null), in which case it prints "null" without trying to call any method on it.

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
I agree. Here's the link:
subject: A println() peculiarity...
It's not a secret anymore!