The NullPointerException

4 October 2007, by: Arjan Tijms

The NullPointerException in Java (often abbreviated as NPE) is a frequently occurring exception in Java. As most of you probably know, it simply means that you (implicitly) try to dereference a pointer that has the special reserved null value.

As it’s a frequently occurring exception every Java programmer must have stumbled upon it at some point in time. It’s simply unavoidable.

So, why this rather trivial introduction? Well, as it seems, some 12 years after the introduction of the Java programming language and thus some 12 years after the introduction of the NullPointerException, it remains an urban myth among some groups of people that Java does not have pointers.

That’s right, some people honestly still think that Java does not have pointers. There have been many discussions about this subject throughout the years (just do a little searching on the internet). The outcome is always the same. Java (of course) does have pointers. Better yet, everything except primitives is handled by a pointer. It’s just not possible to do any arithmetic with them.

So how come this misconception still exists? I can understand that there might have been some confusion when Java was just released, but nearly 12 years later? Don’t those people ever look at the java.lang.NullPointerException and wonder why it says ‘pointer’ ?

Arjan Tijms

7 comments to “The NullPointerException”

  1. Matt says:

    What people mean when they say that Java doesn’t have pointers is that Java doesn’t have C/C++-style pointers.  For example, Wikipedia’s article on "Pointer" says, "sometimes people use the word “pointer” to refer to references in
    general; however, more properly it only applies to data structures
    whose interface explicitly allows it to be manipulated as a memory
    Under the latter definition, Java has references but not pointers.Still, trying to pin down exact universally-agreed definitions for these things is not hugely interesting. More interesting is the observation that Java’s approach rules out a whole class of bugs that can occur with raw C pointers.

  2. arjan says:

    Matt, I’ve heard the argument before. Indeed, it would be a true statement when people just say "Java doesn’t have C/C++ pointers". Of course, the exact properties of the pointer implementation in C/C++ can hardly be taken for the absolute one-and-only definition of the term ‘pointer’.


    On top of that, I believe that the C++ standard never guarantees that a pointer actually -is- a physical memory address. It might be in many implementations, but it certainly does not have to be. The only thing that the standard defines is certain arithmetic operations on it, that indeed make it feel as-if we are directly working with raw memory addresses (to further complicate matters, in a modern operating system the term ‘raw memory address’ also doesn’t mean that much anymore).


    We indeed often say Java has references; it’s the confusing Java terminology. Namely, if we want to stick to C++ nomenclature then ‘reference’ has a whole different meaning. I.e. in C++ the term reference is more closely related to what other languages may call an alias.


    In Java, a pointer can be null (point to nothing), can be reassigned a new value and can be stored. That are (in my humble opinion) the major usecases for a pointer. Taking this into another direction; in C++ you don’t -have- to do pointer arithmetic. In fact, many casual/higher level C++ programmers prefer not to use it at all. Yet, you would still say they were using pointers.


    I absolutely agree with you about the observation that Java’s approach rules out a whole class of bugs that can occur with raw C pointers. You sacrifice some power and gain a lot of guarantees.

  3. mb says:

    Java has since 12 years References to objects – no Pointers

  4. KiLVaiDeN says:


    NPE is in my opinion not the right name for that kind of exception. NullObjectException would have been better imo.

    Java makes it clear that you can’t talk about pointers when you code with it. I find it just confusing ( hence your article ) that they called it a NullPointerException, since you ain’t supposed to know what a pointer is when coding in Java.

    My 2 cents, and I agree with mb somehow !

  5. arjan says:

    >since you ain’t supposed to know what a pointer is when coding in Java.


    Why not? As long as you can have multiple pointers to the same Object, you have to understand the pointer concept.


    A programmer has to understand that using any of these pointers leads to the same Object and that any of these pointers can be re-assigned to point to a different Object. This programmer also has to know that an array of Objects in Java does not actually hold the Objects themselves, but merely pointers to these Objects (as opposed to an array of primitives which does hold the primitives directly).


    This alone is more than enough justification for having to know about  the pointer concept.


    Also, SCJP preparation books (e.g. the ones made by the same people who also create the exam), spend time explaining pointers. Now if a Java programmer isn’t supposed to know about pointers, why does she have to learn them for the basic Sun Java certification? 

  6. KiLVaiDeN says:

    Arjan, sorry for the late answer.

    First of all : you don’t manipulate pointers in Java. You manipulate “aliases” of the original pointer. Each objet has only _ONE_ pointer assigned to it, and you can alias this pointer with other names, but there is never the need to understand what the real concept of a “pointer” is.

    In case you wonder why I say that, take the C pointer as an example. C lets you really manipulate the pointer, because it’s not binded with any object, you can move it in the memory and point somewhere else explicitly. It’s NOT the case in Java, hence my comment.

    When you receive a NPE from the JVM, Java is telling you : “The alias you are using doesn’t any pointer”, in other words “no object is being pointed out by your alias” so basically, I’d have said, from an Object Oriented point of view, that it should be called “NullObjectException” as I said earlier, or something along those lines.

    Maybe “AccessingNullObjectException” ?


  7. KiLVaiDeN says:

    I think my post wasn’t that clear : what I really meant to say in the end is that the concept of pointer comes with the concept of “pointer arithmetic”, which you don’t have in Java. You obtain aliases which can reference the original pointer, but that’s it.

    Oh and about the Basic Certification of Java, talking about pointers, it’s also important to notice that this Certification doesn’t aim expert Java programmers, but beginners. It’s good to teach them what a pointer is without getting into the details as I said earlier.


Type your comment below:

Time limit is exhausted. Please reload CAPTCHA.

css.php best counter