Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this not throwing a NullPointerException?

Nead clarification for following code:

StringBuilder sample = new StringBuilder(); StringBuilder referToSample = sample; referToSample.append("B"); System.out.println(sample); 

This will print B so that proves sample and referToSample objects refer to the same memory reference.

StringBuilder sample = new StringBuilder(); StringBuilder referToSample = sample; sample.append("A"); referToSample.append("B"); System.out.println(referToSample); 

This will print AB that also proves the same.

StringBuilder sample = new StringBuilder(); StringBuilder referToSample = sample; referToSample = null; referToSample.append("A"); System.out.println(sample); 

Obviously this will throw NullPointerException because I am trying to call append on a null reference.

StringBuilder sample = new StringBuilder(); StringBuilder referToSample = sample; referToSample = null; sample.append("A"); System.out.println(sample); 

So Here is my question, why is the last code sample not throwing NullPointerException because what I see and understand from first two examples is if two objects referring to same object then if we change any value then it will also reflect to other because both are pointing to same memory reference. So why is that rule not applying here? If I assign null to referToSample then sample should also be null and it should throw a NullPointerException but it is not throwing one, why?

like image 498
commit Avatar asked Sep 05 '13 12:09

commit


People also ask

How do you throw NullPointerException?

NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

When should we throw a NullPointerException?

NullPointerException is thrown when program attempts to use an object reference that has the null value. These can be: Invoking a method from a null object. Accessing or modifying a null object's field.

Why NullPointerException should not be caught?

It is generally a bad practice to catch NullPointerException. Programmers typically catch NullPointerException under three circumstances: The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem.


1 Answers

null assignments do not change value by globally destroying that object. That kind of behavior would lead to hard-to-track bugs and counterintuitive behavior. They only break that specific reference.

For simplicity, let's say that sample points to address 12345. This is probably not the address, and is only used to make things simple here. The address is typically represented with the weird hexadecimal given in Object#hashCode(), but this is implementation-dependent.1

StringBuilder sample = new StringBuilder(); //sample refers to  //StringBuilder at 12345   StringBuilder referToSample = sample; //referToSample refers to  //the same StringBuilder at 12345  //SEE DIAGRAM 1  referToSample = null; //referToSample NOW refers to 00000,  //so accessing it will throw a NPE.  //The other reference is not affected. //SEE DIAGRAM 2  sample.append("A"); //sample STILL refers to the same StringBuilder at 12345  System.out.println(sample); 

From the lines marked See diagram the diagrams of the objects at that time are as follows:

Diagram 1:

[StringBuilder sample]    -----------------> [java.lang.StringBuilder@00012345]                                                       ↑ [StringBuilder referToSample] ------------------------/ 

Diagram 2:

[StringBuilder sample]    -----------------> [java.lang.StringBuilder@00012345]  [StringBuilder referToSample] ---->> [null pointer] 

Diagram 2 shows that annulling referToSample does not break the reference of sample to the StringBuilder at 00012345.

1GC considerations make this implausible.

like image 117
nanofarad Avatar answered Nov 15 '22 12:11

nanofarad