• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointer Doubt.  RSS feed

 
Yogendra Joshi
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

I have a general doubt on Passing Objects as reference in Java. Given below code :



In above, if i add a key / value pair inside modifyHashMap method, the modified value is reflected in main method, However given below code :



Why in the above code , after "Line to check", i am explicitly setting it to NULL, why isnt it throwing NullPointerException ? I am very confused since when i add it reflects my changes , however when i set to Null it doesnt ?

Thanks and Regards,
Yogendra N Joshi.
 
Suren Singh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understand one fact , that in java method arguments are passed by value, and when object references are passed,
they too get passed as copies or by value .

So in first case, as the reference was pointing to the same object, it manipulated the original object and hence the output.

In second case when you were setting it to null, the copy of the actual reference was set to null, no effect on the original
reference. So no null pointer

Hope it helps

Thanks
Suren
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!