You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Scott Sanders <ss...@nextance.com> on 2002/09/14 02:20:14 UTC

[Collections] Array Stack throws NPE on push() but not add()

I am trying to use ArrayStack as a sort of sparse stack (most elements
are null), and noticed that push guards against null, whereas add() does
not.  For example:

1. stack.push(null) throws NPE
2. stack.add(null) works fine

Which one is incorrect?  I believe that these should be symmetric, but
which way? I actually don't want to guard against null in either.  If
the decision is for guarding, do I just extend and override or does it
have a place in collections?

Cheers,
Scott Sanders 

Perfection is achieved, not when there is nothing more to add, but when
there is nothing left to take away. - Antoine de Saint-Exupery


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [Collections] Array Stack throws NPE on push() but not add()

Posted by Steve Downey <st...@netfolio.com>.
I can't see any reason for it to throw an exception. In fact, it even has a 
specific check for == null, and throws the exception specifically.

In general collections should only throw a null pointer exception when it has 
to. And then as a side effect of calling a method on a null object.

On Friday 13 September 2002 08:20 pm, Scott Sanders wrote:
> I am trying to use ArrayStack as a sort of sparse stack (most elements
> are null), and noticed that push guards against null, whereas add() does
> not.  For example:
>
> 1. stack.push(null) throws NPE
> 2. stack.add(null) works fine
>
> Which one is incorrect?  I believe that these should be symmetric, but
> which way? I actually don't want to guard against null in either.  If
> the decision is for guarding, do I just extend and override or does it
> have a place in collections?
>
> Cheers,
> Scott Sanders
>
> Perfection is achieved, not when there is nothing more to add, but when
> there is nothing left to take away. - Antoine de Saint-Exupery


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>