Since there are no pointers in Java why is there aNullPointerException?

It is a bit misleading to say that Java does not have pointers.Reference variables are very similar to automatically dereferencedpointers, but act in a safer manner. It is more accurate to say thatJava does not support pointer arithmetic or access to raw memory.Reference variables still need to point to real objects, even thoughthey do not point to raw memory. An uninitialized reference variableholds the null reference value, which is a special value meaningthat the variable does not reference anything. A referencevariable can also be made to no longer reference an object byassigning it the null value.

Any time you try to invoke a method, oraccess a member variable of a null reference, the JVM will throw aNullPointerException. This exception could just as easily been calledNullReferenceException. Java’s designers probably opted for the firstname because references are very closely related to pointers, and C/C++programmers are used to the idea of attempting to reference a nullpointers.


Share the Post: