• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointer Doubt.

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/CallByReferenceVsCallByValue
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic