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 A println() peculiarity... 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 "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: 3591
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...
jQuery in Action, 3rd edition