I am new to Java and getting an error for the following case:
markerObj = null;
markerObj.sections = RowData[1];
But, after removing the first line, it works fine.
What is the issue?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Variable names are only references. They point to a space in memory (in the heap) which contains an object.
When you do
MyObj myObject = new MyObj();, it does two things: create the object in memory, and point the myObject reference to it.So when you do
myObject.sections, it follows the reference, and check the sections part of your object in memory.Then when you do
myObject = null, you basically destroy the link between your reference and your object in memory. Hence when doingmyObject.sections, Java cannot follow the reference anymore because it’s not pointing to anything. So it throws aNullPointerExceptionNote that the object itself is not destroyed by
myObject = null, only the reference is nullified. The Garbage Collector then detects that your object is unreachable (if there’s no other reference to it) and reclaims the memory, destroying the object.