• Post Reply Bookmark Topic Watch Topic
  • New Topic

Null and toString()  RSS feed

 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I want to ask about the following code snippet


My question is ,in the first case,toString() is invoked implicitly on null,and it compiled and ran just fine,but then what is wrong with second line.

Thanks...
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
toString() is only called implicitly if the object passed isn't null. If it is, the literal "null" is printed instead.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhanshu Mishra wrote:Hi all,
I want to ask about the following code snippet


My question is ,in the first case,toString() is invoked implicitly on null,


No, it's not. Somewhere down in one of the methods that println() calls, there's code that's something like

but then what is wrong with second line.


You're dereferencing a null pointer.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!