2

I am been having a great curiosity.

I have written some java code & it is showing Null Pointer Exception. I am curious to know Why java gives Null Pointer Exception even if it does not support pointers?

Green goblin
  • 9,898
  • 13
  • 71
  • 100
  • 5
    Admittedly, it could have been named: `NullReferenceException`... – assylias Aug 10 '12 at 12:56
  • @Aashish - legacy name may be as Java is written in C/C++? – Eugene Aug 10 '12 at 12:56
  • 1
    and C doesn't have a NULL pointer exception. that's truly unfair :P – PypeBros Aug 10 '12 at 12:57
  • Related: http://stackoverflow.com/questions/7436581/what-is-the-difference-between-a-pointer-and-a-reference-variable-in-java – assylias Aug 10 '12 at 12:59
  • 1
    @sylvainulg sure it does, it is just named "segmentation fault" or similarly. – Thorbjørn Ravn Andersen Aug 10 '12 at 13:04
  • Possible duplicates: http://stackoverflow.com/questions/4648476/why-does-java-have-nullpointerexception-instead-of-nullreferenceexception http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception http://stackoverflow.com/questions/101072/java-why-arent-nullpointerexceptions-called-nullreferenceexceptions – munyengm Aug 10 '12 at 13:04
  • @ThorbjørnRavnAndersen: I wasn't expecting a reply on that one, but imvho, SEGFAULT is not part of C: it's something the OS stacks over the language. When programming your own OS or on a system that has no hardware memory protection (embedded systems), you "can" happily follow the NULL pointer, and observe the chaos of multiple part of your code sharing the same buffer for different purposes >_ – PypeBros Aug 10 '12 at 13:08
  • @sylvainulg real men do not need hardware protection ;) – Thorbjørn Ravn Andersen Aug 10 '12 at 13:11

5 Answers5

5

the fact that there's no pointer arithmetic doesn't mean there's no pointer used in the language. And when it comes to Java, there is a lot of pointers, you just follow references with . rather than with ->.

PypeBros
  • 2,607
  • 24
  • 37
1

Object references are nothing but pointers but they are not complex as Pointers. Have a look at this for difference.

Community
  • 1
  • 1
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
1

Java does support pointers (which you use everytime you have a . or a [).

It doesn't support pointer arithmetic - i.e. the ability to manipulate a pointer into another pointer using math.

To point this out to converted C/C++ programmers, they did not carry the C name over, but named it something else. At the JVM level everything is pointers, but as Java programmers only see this when using references, the exception is badly named.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
1

Java does indeed have pointers. They can have two kinds of values: (a) references to objects, or (b) null. This is the language used in the Specification.

Java pointers are however much more akin to Pascal pointers than to C pointers.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

It's a carryover from java's "C" herritage. It's also the parlance of the trade.

Bohemian
  • 412,405
  • 93
  • 575
  • 722