Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java have NullPointerException instead of NullReferenceException? [duplicate]

Tags:

java

naming

Possible Duplicates:
What is a Null Pointer Exception?
Java: Why aren't NullPointerExceptions called NullReferenceExceptions ?

Our of idle curiosity, does anyone know why the null reference exception in Java was called NullPointerException?

It seems counterintuitive that in a new language that officially has no pointers a choice would be made to use this name when using a null reference.

If anyone can point me to an authoritative explanation, that would be appreciated.

like image 275
Uri Avatar asked Jan 10 '11 15:01

Uri


People also ask

What is the reason for NullPointerException in Java?

What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.

How do I fix NullPointerException in Java?

In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.

What is a null reference exception in Java?

A NullReferenceException exception is thrown when you try to access a member on a type whose value is null . A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios: You've forgotten to instantiate a reference type.

Does isEmpty throw NullPointerException?

Exception for of isEmpty() in JavaThe isEmpty() method throws a NullPointerException when the string is initialized to null.


1 Answers

The statement that Java "officially has no pointers" is simply false. Just because you can't do arithmetic on it doesn't mean it's not a pointer. C is not the final authority for programming terminology.

The only people who get hung up over it are either C/C++ fans who want to disparage Java for its lack of power over the bare metal, or (unlikely, nowadays) marketing people who want to sell Java as a simpler, safer alternative to managers who've had bad experiences with C development.

From the Java language specification:

The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

like image 60
Michael Borgwardt Avatar answered Sep 22 '22 08:09

Michael Borgwardt