Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A println() peculiarity...

 
Matthew Alesi
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3776
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic