Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why NullPointerException instead of NullReferenceException?

605 views
Skip to first unread message

vgane...@my-deja.com

unread,
Oct 29, 1999, 3:00:00 AM10/29/99
to venkat...@netscape.net
Given that Java does NOT support pointers, why do we have
java.lang.NullPointerException instead of NullReferenceException?

Thanks.


Sent via Deja.com http://www.deja.com/
Before you buy.

Hydra

unread,
Oct 29, 1999, 3:00:00 AM10/29/99
to
In article <7vcebd$s90$1...@nnrp1.deja.com>, vgane...@my-deja.com
barfed...

> Given that Java does NOT support pointers, why do we have
> java.lang.NullPointerException instead of NullReferenceException?

What's in a name? Some call 'em pointers, some call 'em references. Don't
forget, most of the dudes that wrote the API where probably seasoned C++
coders. :-)

Niels
--
/*
* Mail hydr...@wxs.nl
* "One Ring to rule them all, One Ring to find them,
* One Ring to bring Them all and in the darkness bind them
* In the Land of Mordor where the Shadows lie."
*/

Biju Thomas

unread,
Oct 29, 1999, 3:00:00 AM10/29/99
to
vgane...@my-deja.com wrote:
>
> Given that Java does NOT support pointers, why do we have
> java.lang.NullPointerException instead of NullReferenceException?

Java HAS pointers - they are handles to objects. Some call them
references, but, that doesn't mean that Java has no pointers.

--
Biju Thomas

Carl L. Gay

unread,
Oct 29, 1999, 3:00:00 AM10/29/99
to
vgane...@my-deja.com wrote:
>
> Given that Java does NOT support pointers, why do we have
> java.lang.NullPointerException instead of NullReferenceException?

Actually it should be called NullDereferencedException,
whether or not Java has pointers. ;-)

cbu...@my-deja.com

unread,
Oct 30, 1999, 3:00:00 AM10/30/99
to
In article <3819DE5C...@thecia.net>,

Very true. Its funny that I had this same discussion a few days ago.

A reference is a pointer, except that math operations on it are not
allowed.

--
CB

vgane...@my-deja.com

unread,
Nov 1, 1999, 3:00:00 AM11/1/99
to venkat...@netscape.net
There are some differences between pointers and reference.

From C++ Programming Lang., by Stroustrup the definition for a
reference is : "A reference is an alternative name for an object".

According to the Java Language specification, a variable of
reference type holds a reference to an object.

Using reference you can change the data members in that object.
However, you cannot access the memory location.

Using pointers, you can access the memory locations occupied by that
object and also change the data members in that object.

In the Java Lang. Env. white paper, James Gosling, father of Java says:

2.2.9 No More Pointers
Most studies agree that pointers are one of the primary features that
enable programmers to inject bugs into their code. Given that
structures are gone, and arrays and strings are objects, the need for
pointers to these constructs goes away. Thus, Java has no pointer data
types. Any task that would require arrays, structures, and pointers in
C can be more easily and reliably performed by declaring objects and
arrays of objects. Instead of complex pointer manipulation on array
pointers, you access arrays by their arithmetic indices. The Java run-
time system checks all array indexing to ensure indices are within the
bounds of the array.

You no longer have dangling pointers and trashing of memory because of
incorrect pointers, because there are no pointers in Java.

** end of quote **

It is very clear that designers of the Java Programming language, has
made a conscious decision to get away from pointers.

This leads us back to the original question :

Given that Java does NOT support pointers, why do we have
java.lang.NullPointerException instead of NullReferenceException?

Is NullPointerException a misnomer ?

Thanks.

Patricia Shanahan

unread,
Nov 1, 1999, 3:00:00 AM11/1/99
to

vgane...@my-deja.com wrote:
...


> Given that Java does NOT support pointers, why do we have
> java.lang.NullPointerException instead of NullReferenceException?
>
> Is NullPointerException a misnomer ?

Nope - it is the claim that Java doesn't have pointers that is the
problem. Once you realise that it is thick with pointers, but without
the dangerous C innovations of pointer arithmethic and arbitrary
conversion, life gets a lot simpler, and NullPointerException makes
perfect sense.

Steve Chapel

unread,
Nov 1, 1999, 3:00:00 AM11/1/99
to
vgane...@my-deja.com wrote:
>
> There are some differences between pointers and reference.
>
> From C++ Programming Lang., by Stroustrup the definition for a
> reference is : "A reference is an alternative name for an object".

Yes, that's what a *C++* reference is. A *Java* reference is very
different from a C++ reference. To give two examples of these
differences, C++ does not allow a "null" reference; all references
do reference an actual object. Java allows null references. In C++,
initializing a reference is very different from assigning to a
reference. In Java, initializing and assigning have the same effect.
Comparing C++ and Java references is like comparing apples and oranges.
It will only lead to the confusion that this post falls into...


> According to the Java Language specification, a variable of
> reference type holds a reference to an object.
>
> Using reference you can change the data members in that object.
> However, you cannot access the memory location.

But you can change the data members by writing to the memory location,
and by writing to the memory location, you change the data members.
These two terms are simply two different ways of describing the same
thing. Why do you think they are different?


> Using pointers, you can access the memory locations occupied by that
> object and also change the data members in that object.

Yes, just as you can with Java references, and just as you can with C++
pointers.


> In the Java Lang. Env. white paper, James Gosling, father of Java says:
>
> 2.2.9 No More Pointers
> Most studies agree that pointers are one of the primary features that
> enable programmers to inject bugs into their code. Given that
> structures are gone, and arrays and strings are objects, the need for
> pointers to these constructs goes away. Thus, Java has no pointer data
> types. Any task that would require arrays, structures, and pointers in
> C can be more easily and reliably performed by declaring objects and
> arrays of objects. Instead of complex pointer manipulation on array
> pointers, you access arrays by their arithmetic indices. The Java run-
> time system checks all array indexing to ensure indices are within the
> bounds of the array.
>
> You no longer have dangling pointers and trashing of memory because of
> incorrect pointers, because there are no pointers in Java.
>
> ** end of quote **
>
> It is very clear that designers of the Java Programming language, has
> made a conscious decision to get away from pointers.

Yes, in the sense that many operations that C and C++ allow on pointers
do not exist in Java. In C and C++, pointers are merely glorified
machine language addresses. In Java, pointers have far fewer
capabilities,
and cannot be abused nearly as severely.


> This leads us back to the original question :
>

> Given that Java does NOT support pointers, why do we have
> java.lang.NullPointerException instead of NullReferenceException?

Java absolutely does support pointers! They act differently from
pointers
in other languages, especially C and C++, and apparently Gosling wanted
a new term to differentiate what Java has from other languages'
pointers. I think in the long run, this decision has led to more
confusion that clarity.

A Java NullPointerException, as demonstrated in the following code, is
much closer to C++ dereferencing a null pointer than anything C++ does
with references:

Java: String s = null; s.equals(""); // throws NullPointerException
C++: String *s = 0; s->equals(""); // dereferencing of a null pointer

Both C++ and Java support passing a pointer to a method so that method
can change memory locations that the pointer points to:

Java: void foo(MyClass m) { m.field = 3; }
C++: void foo(MyClass *m) { m->field = 3; }

Note that passing a reference to a C++ method allows something much
different than what is allowed in Java:

C++: void swap(Object &a, Object &b) { Object t = a; a = b; b = t; }

Try to write code like that using Java references, using the method
signature void swap(Object a, Object b). You can't!

However, you can write a swap method by using a two-element
array to simulate passing pointers:

Java: void swap(Object o[]) { Object t = o[0]; o[0] = o[1]; o[1] = t; }
C++: void swap(Object *o[]) { Object *t = o[0]; o[0] = o[1]; o[1] = t;
}

Could you post *in your own words* why you believe that Java references
are more like C++ references than C++ pointers?

vgane...@my-deja.com

unread,
Nov 2, 1999, 3:00:00 AM11/2/99
to venkat...@netscape.net
Steve,

Thanks for the reply. I will try to explain, some of the questions you
have raised, in my own words.

In article <381E6B24...@cs.uiowa.edu>,


Steve Chapel <sch...@cs.uiowa.edu> wrote:
> vgane...@my-deja.com wrote:
> >
> > There are some differences between pointers and reference.
> >
> > From C++ Programming Lang., by Stroustrup the definition for a
> > reference is : "A reference is an alternative name for an object".
>
> Yes, that's what a *C++* reference is. A *Java* reference is very
> different from a C++ reference. To give two examples of these
> differences, C++ does not allow a "null" reference; all references
> do reference an actual object. Java allows null references. In C++,
> initializing a reference is very different from assigning to a
> reference. In Java, initializing and assigning have the same effect.
> Comparing C++ and Java references is like comparing apples and
oranges.
> It will only lead to the confusion that this post falls into...
>

Java has borrowed a lot of good stuff, from many languages. In C++,
experts recommend passing parameters by reference, to avoid the
overhead of copying big objects. I think, Java has borrowed the concept
of reference from C++.

You have already pointed out the difference between C++ reference
variables and Java reference variables. I agree.

There is a lot of similarities between C++ reference variables and Java
reference variables. In both languages, the reference variables are
used to refer to some object, and then, that object is manipulated
using the reference variable. This particular usage of the reference
variable, is rampant in both C++ and Java worlds.


> > According to the Java Language specification, a variable of
> > reference type holds a reference to an object.
> >
> > Using reference you can change the data members in that object.
> > However, you cannot access the memory location.
>
> But you can change the data members by writing to the memory location,
> and by writing to the memory location, you change the data members.
> These two terms are simply two different ways of describing the same
> thing. Why do you think they are different?
>

In Java, pointers are NOT available. Memory locations CANNOT be
accessed in Java. The data members of an object, can be accessed,by the
Java programmer, if and only if, the corresponding class allow access
to that data member.

> > Using pointers, you can access the memory locations occupied by that
> > object and also change the data members in that object.
>
> Yes, just as you can with Java references, and just as you can with
C++
> pointers.

Using Java references, a Java programmer cannot access memory
locations. In fact, a Java programmer does not have access to memory
locations. If you think otherwise, please post a Java program, that
access some memory locations.

Pointers are different from reference. The pointer variable will
usually point to some memory location. A reference variable usually
will point to some object, in memory.

Pointers are powerful and are like two-edged sword. If we don't use
them properly, it can hurt. There are many C/C++ libraries that produce
a core dump because it is not correctly written and/or correctly
used/applied.

With Java reference variables, chances of corrupting the memory,
overwriting, underwriting etc. are very less or nil.

>
> A Java NullPointerException, as demonstrated in the following code, is
> much closer to C++ dereferencing a null pointer than anything C++ does
> with references:
>
> Java: String s = null; s.equals(""); // throws NullPointerException

In this Java program, s is a reference variable.

> C++: String *s = 0; s->equals(""); // dereferencing of a null
pointer

In this C++ program, s is a pointer. Very powerful, be careful ;-)!

In Java there are only two types of variables, viz. primitive variables
and reference variable.

Examples for primitive variables : int i=10; char c='a';

There are only three types of reference variables, in Java. They are
class reference variable, interface reference variable and array
reference variable.

All reference variables, in Java, will be null or they will point to
some heap based object only. In other words, Java reference variables
CANNOT refer to a stack based object. In Java, objects are always
created in the heap only.

>
> Both C++ and Java support passing a pointer to a method so that method
> can change memory locations that the pointer points to:
>

In Java, the parameters (primitives, as well as, reference) to a
method, are always passed-by-value only. The actual parameters value
are copied into the corresponding formal parameters and then the
instructions continue.

> Java: void foo(MyClass m) { m.field = 3; }
> C++: void foo(MyClass *m) { m->field = 3; }
>
> Note that passing a reference to a C++ method allows something much
> different than what is allowed in Java:
>
> C++: void swap(Object &a, Object &b) { Object t = a; a = b; b = t; }
>
> Try to write code like that using Java references, using the method
> signature void swap(Object a, Object b). You can't!
>

If I am asked to write a swap program, in Java, to swap two int's,
it'll look somewhat like this.

>cat Swap.java
// java program Swap.java
import java.awt.Point;
class Swap {
public static void main (String [] args ) {
Point p=new Point (10,20);
System.out.println ( "Before p.x = " + p.x + " p.y = "+ p.y);
swap ( p );
System.out.println ( "After p.x = " + p.x + " p.y = " + p.y);
} // end main
static void swap ( Point p2) {
int temp=p2.x;
p2.x=p2.y;
p2.y=temp;
} // end swap
} // end class

>java Swap
Before p.x = 10 p.y = 20
After p.x = 20 p.y = 10

The key point, in this example program, is the actual parameter 'p' is
passed by value, to the formal parameter, 'p2'. Now the formal
parameter and the actual parameter are actually referring to the same
object. This object data members are changed inside swap(). After swap
() completes, we see the changed values.

> However, you can write a swap method by using a two-element
> array to simulate passing pointers:
>
> Java: void swap(Object o[]) { Object t = o[0]; o[0] = o[1]; o[1] = t;
}
> C++: void swap(Object *o[]) { Object *t = o[0]; o[0] = o[1]; o[1] =
t;
> }
>
> Could you post *in your own words* why you believe that Java
references
> are more like C++ references than C++ pointers?
>

To summarize, I feel Java reference and C++ reference are used, with
almost similar motive. The references are used manipulate the objects
they are referring to. Using Java reference we can manipulate the
object, not the memory content.

While a pointer points to a memory location, a reference variable
refers to an object in memory. A reference variable can be thought of
an alias.

Using pointers, programmers can corrupt memory and bring the program to
a scratching halt (core dump).

Using references, keeping the data members private and performing
validation, in the accessor functions, the objects can be kept healthy
and in good shape.

vgane...@my-deja.com

unread,
Nov 2, 1999, 3:00:00 AM11/2/99
to venkat...@netscape.net
In article <381E4DF2...@acm.org>,
Patricia Shanahan <pa...@acm.org> wrote:
>
>
> vgane...@my-deja.com wrote:
> ...

> > Given that Java does NOT support pointers, why do we have
> > java.lang.NullPointerException instead of NullReferenceException?
> >
> > Is NullPointerException a misnomer ?
>
> Nope - it is the claim that Java doesn't have pointers that is the
> problem. Once you realise that it is thick with pointers, but without
> the dangerous C innovations of pointer arithmethic and arbitrary
> conversion, life gets a lot simpler, and NullPointerException makes
> perfect sense.

Patricia,
Thanks for the reply.

In the Java Lang. Env. white paper, James Gosling, says:
"2.2.9 No More Pointers".

Do you think he is lying ?;-)!
Can you please explain, why you think Java is thick with pointers ?
Thanks a lot.

Peter van der Linden

unread,
Nov 2, 1999, 3:00:00 AM11/2/99
to
>In the Java Lang. Env. white paper, James Gosling, says:
>"2.2.9 No More Pointers".
>
>Do you think he is lying ?;-)!
>Can you please explain, why you think Java is thick with pointers ?

Well, for one thing, the Java Language Specification says Java has
pointers. That is the official document that defines the Java language.
A white paper is just, well, a white paper.

Could you provide a URL for the paper? I bet it is one of the early
papers where the Java team was still trying hard to distinguish Java
from C++. This "no pointers" statement is a misleading (and actually
wrong) over-simplification from that time.

---


The Java FAQ is at
http://www.afu.com

Might be worth bookmarking it, or looking thru it for other info.

Steve Chapel

unread,
Nov 2, 1999, 3:00:00 AM11/2/99
to
vgane...@my-deja.com wrote:
>
> Steve,
>
> Thanks for the reply. I will try to explain, some of the questions you
> have raised, in my own words...

I'll try to summarize what you've stated. Your entire argument that Java
references are not like C pointers is that C pointers can point to
*arbitrary* locations in memory, while Java references must point to
actual objects, and these objects are always on the heap.

This difference is a consequence of the fact that Java does not allow
casting an integer to a reference, or adding or subtracting integers
to or from references. Java also does not allow the programmer the
choice whether or not to dereference a reference; you cannot assign
one Java object to another, since you can't do the C equivalent of
Object *a = new Object(), *b = new Object(); *a = *b;

A C++ reference on the other hand, acts radically differently in many
ways. A Java reference is a restricted C or C++ pointer. Your claim
that you cannot change memory locations in Java is completely
incorrect. Any time you change the value of any variable or field,
you are changing a memory location.

I find that when I program Java code, I think in terms of pointers.
I rarely get confused. I've been in this newsgroup long enough to
notice that when other programmers try thinking in terms of C++
references, somewhere around half the time they get hopelessly
confused. Usually that confusion is lifted when someone explains
in terms of pointers.

In addition, recent Java documentation states directly that Java
references are, in fact, pointers.

All I can say is that your claim that Java references are not pointers
is shaky at best. Don't be surprised if other posters disagree with
or get confused by your posts.

Dale King

unread,
Nov 3, 1999, 3:00:00 AM11/3/99
to
Basically it all comes down (as most arguments do) to a mattere of
definitions. It is as silly as arguing over whether Java has
frinkleheims. Unless we agree on a definition of what a frinkleheim is,
it is pointless to argue about it.

So, does Java have pointers? Define what you mean by pointer and I can
give you an answer. There are many interpretations of what is meant by
pointer. It certainly doesn't have C style pointers, but they aren't C++
references either.

Rather than trying to see if Java molds to some other definition it
makes more sense to just learn what Java does have.

Steve Chapel wrote:


>
> vgane...@my-deja.com wrote:
> >
> > Thanks for the reply. I will try to explain, some of the questions you

--
--- Dale King

vgane...@my-deja.com

unread,
Nov 4, 1999, 3:00:00 AM11/4/99
to venkat...@netscape.net
Thanks for pointing out Java FAQ site.

The Java Language Environment - A White Paper - by James Gosling is
available at

http://java.sun.com/docs/white/langenv/

I was referring to the section "2.2.9 No More Pointers"

Steve Chapel

unread,
Nov 4, 1999, 3:00:00 AM11/4/99
to
Dale King wrote:
>
> Basically it all comes down (as most arguments do) to a mattere of
> definitions. It is as silly as arguing over whether Java has
> frinkleheims. Unless we agree on a definition of what a frinkleheim is,
> it is pointless to argue about it.
>
> So, does Java have pointers? Define what you mean by pointer and I can
> give you an answer. There are many interpretations of what is meant by
> pointer. It certainly doesn't have C style pointers, but they aren't C++
> references either.
>
> Rather than trying to see if Java molds to some other definition it
> makes more sense to just learn what Java does have.


But we all have to agree on what terms mean in order to communicate
effectively.

This pointer vs. reference thread reminded me of when I worked in a
computer lab several years ago. A couple of people called 3 1/2"
floppy disks "hard disks". When I explained that they were in fact
floppy disks, they would tap the disk on the table and say "no, it's
a hard disk."

I guess they can use any term at all to refer to what those disks were,
even "frinkleheims!" But they are going to confuse themselves and others
by doing so.

Since communication and understanding are central to this newsgroup,
it's actually very important to agree on terminology.

If you program Java and think about references in terms of C++
references,
you're much more likely to get confused than if you think about them
in terms of C++ pointers.

Dale King

unread,
Nov 8, 1999, 3:00:00 AM11/8/99
to Steve Chapel
Steve Chapel wrote:
>
> Dale King wrote:
> >
> > Basically it all comes down (as most arguments do) to a mattere of
> > definitions. It is as silly as arguing over whether Java has
> > frinkleheims. Unless we agree on a definition of what a frinkleheim is,
> > it is pointless to argue about it.
> >
> > So, does Java have pointers? Define what you mean by pointer and I can
> > give you an answer. There are many interpretations of what is meant by
> > pointer. It certainly doesn't have C style pointers, but they aren't C++
> > references either.
> >
> > Rather than trying to see if Java molds to some other definition it
> > makes more sense to just learn what Java does have.
>
> But we all have to agree on what terms mean in order to communicate
> effectively.

> Since communication and understanding are central to this newsgroup,


> it's actually very important to agree on terminology.

Boy, you must be new here ;-) I can't recall seeing too much effective
communication in a newsgroup.

I was just getting tired of people disagreeing with other when the
difference was that they had different meaning for the word pointer. It
was pretty much:

A: Java has frinkleheims.
B: No it doesn't, it has jinglefrotz.
--
--- Dale King

0 new messages