1

We don't talk about pointers in Java, so why do we use the term NullPointerException in Java? I think the exception name NullReferenceException makes more sense.

Duncan Jones
  • 59,308
  • 24
  • 169
  • 227
Ishan Rastogi
  • 690
  • 1
  • 7
  • 16
  • Except that each Object variable you define is actually a pointer. – Ceiling Gecko Mar 25 '14 at 09:23
  • There are no pointers in java. The things you can do i.e. in c++ with pointers cannot be compared to those you can do in java with references – Markus Mar 25 '14 at 09:25
  • 4
    possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) and [Java: Why aren't NullPointerExceptions called NullReferenceExceptions?](http://stackoverflow.com/questions/101072/java-why-arent-nullpointerexceptions-called-nullreferenceexceptions) – Yehor Nemov Mar 25 '14 at 09:25
  • 1
    @ghoshak The second of those duplicates is a perfect match, good spot. The first is not a duplicate at all. – Duncan Jones Mar 25 '14 at 09:28
  • @Duncan thanks for the correction. The first one is about Null Pointer exception concept. It could be useful for understanding the topic. – Yehor Nemov Mar 25 '14 at 09:35

1 Answers1

1

We talk about null pointer exceptions because the exception is named NullPointerException. Why? We will never know for sure ... unless someone from the original design team explains the history1.

The fact that there is an inconsistency really doesn't matter. We all know what is meant. It is interesting to note the Javadoc for NullPointerException sidesteps the issue by just talking about null without using the words "pointer" or "reference".

And before you suggest that the Java team should change the name of the exception, consider the impact that this would have.


1 - My theory is that early on (in the pre-Java 1.0 days) pointer and reference were used somewhat interchangeably. Then they decided use "Java has references not pointers" as a point of difference with C and C++ ... but were left with a core exception that had an anomalous name.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096