Setting a reference to null will not throw NPE , otherwise think about how you would ever invalidate your references ? Also, when you null any link, only that link is separated from the object it points to. But the remaining links will still be there.
For example, for example:
MyClass obj1 = new MyClass(); MyClass obj2 = obj1; obj1 = null; // only obj1 is detached from object System.out.println(obj2); // obj2 still points to original object
So, when you call your method: -
new Test().setNull();
A copy of the link is stored in this (Java does not follow the link , rather, it passes the links by value ), which is then passed to another method again, so another copy is created, which is then set to null. But the original link still points to this object.
NPE can be reset only when trying to call any method or access any properties of an object in a null reference.
source share