Fog Creek Software
Discussion Board




If Java doesn't have pointers...

how come it has a NullPointerException?

Just curious
Wednesday, September 01, 2004

Pass.

They could have created a class called "NullReferenceException" to indicate the same thing.

What I want to know is, does it really matter?

TheGeezer
Wednesday, September 01, 2004

"What I want to know is, does it really matter?"

Does anything?

Marvin
Wednesday, September 01, 2004

just cause you can't manipulate them doesn't mean they ain't there.

Captain Obvious
Wednesday, September 01, 2004

it's just semantics


Wednesday, September 01, 2004

When people say "Java doesn't have pointers", they mean these pointers:

T[] t;
*(t + 5);

etc.

Koz
Wednesday, September 01, 2004

Which is a bit of a b*stard in some ways. If you want to pass an array of "somethings" to a function you also need to pass an offset. In C you could just use a pointer to the place in the array where it starts and Bob's your uncle (again; he gets around a bit does your uncle).


Wednesday, September 01, 2004

Well,  I just pass around java.util.List's.  Haven't used an Array in ages.  But whatever floats your boat ;)

Koz
Wednesday, September 01, 2004

Pass an offset vs a Pointer?  What's the difference?  You're passing one extra parameter either way.

BTW, for people who miss this kind of thing in Java, check out the nio package with it's ByteBuffers.  No pointers but you finally can have a reusable buffer to manipulate.  On many OS you can even use allocateDirect to get a native buffer.  Lot's of the fun with none of the overflows.

name withheld out of cowardice
Wednesday, September 01, 2004

He meant that instead of calling

  SearchNRecordsOfTheArrayStartingAtTheGivenPoint (foo, 5, 10);

you can call

  search_from (foo + 5, 10)

bigoted against java
Wednesday, September 01, 2004

Because Java does have 'references', which are a type-safe way of doing what pointers do for C.  In C, when you throw around pointers, you are actually throwing around actual addresses to variables stored in memory space.  A 'null' pointer actually points to the real location zero in your memory space.  Trying to access that location usually results in a memory permission violation error.

In Java, when you are throwing around references, you are throwing around a run-time-handle to allocated objects.  If you don't properly initialize a reference, it may point to 'nothing' -- but that 'nothing' is NOT a valid address in your memory space.  Java can catch that it is a null reference.

AllanL5
Wednesday, September 01, 2004

From one point of view, all references are really "pointers" behind the scenes.

So, to call it a NullPointerException is correct.

A.F.

Avrom Finkelstein
Wednesday, September 01, 2004

Two words: Leaky Abstraction.

It should be called NullReferenceException.

Almost Anonymous
Wednesday, September 01, 2004

That's NOT a leaky abstraction.

muppet
Wednesday, September 01, 2004

Sure it is...  it's an implementation detail (oh, references are implemented as pointers?! gosh!)  that's peaking through.

Almost Anonymous
Wednesday, September 01, 2004

That's a pretty generous definition of "leaky".

muppet
Wednesday, September 01, 2004

Sure..  but it's also pretty stupid to have a null pointer exception when the entire language specification makes absolutely no mention of the word "pointer".

Almost Anonymous
Wednesday, September 01, 2004

Shall we discuss the stupidities in PHP?  :)

muppet
Wednesday, September 01, 2004

'the entire language specification makes absolutely no mention of the word "pointer"'

Not entirely true - eg from Section 4.3.1:

An object is a class instance or an array.

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

as
Wednesday, September 01, 2004

Hehe..  so they say "...special null reference..." but the exception is still null pointer.

Almost Anonymous
Wednesday, September 01, 2004

"Shall we discuss the stupidities in PHP? "

You mean there's other stuff in there as well? :-)

Just me (Sir to you)
Thursday, September 02, 2004

In all fairness, I believe the original 'idea' of a 'pointer' came from Pascal.  It was C that turned the 'idea' into an implementation where a 'pointer' became a literal address on the machine -- and then made the only way you could change the value of a parameter in a subroutine was to pass the subroutine a pointer to the parameter.

In Pascal, you had 'VAR' parameters, which did the same thing, BUT you simply passed the name of the parameter.  Only the SUBROUTINE needed to be told it was a VAR parameter.

C++ then came up with the idea of a 'reference', which gave to the language a Pascal-type VAR parameter, in effect.

Java then decided the 'C pointer' was too dangerous for implementation, and implemented references only. 

I still think the 'idea' of a 'pointer' is valid.  Maybe with Java we can soon talk about a 'reference' to a variable 'pointing' to the variable, without making people argue about what a 'C pointer' does.

AllanL5
Thursday, September 02, 2004

http://c2.com/cgi/wiki?NullPointerException

Has an interesting discussion on the matter.

Avrom Finkelstein
Thursday, September 02, 2004

The first pointers I saw were in assembler.

crotchety
Friday, September 03, 2004

*  Recent Topics

*  Fog Creek Home