devxlogo

NullPointerException

NullPointerException

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

Answer:
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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist