You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Stephen Colebourne <sc...@btopenworld.com> on 2002/06/29 16:55:17 UTC

[lang] NullArgumentException?

I would like to add a NullArgumentException class to the lang.exceptions
package.

It would be a subclass of IllegalArgumentException.
It would be thrown when null is passed in as an argument when it shouldn't
be.

Any objections?

Stephen


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


HttpClient NullPointerException in equals(Object o) of org.apache.commons.httpclient.NameValuePair

Posted by Rand McNeely <ra...@yahoo.com>.
This is my first contribution to a Jakarta project, if I am in poor
form, let me know.

Before calling object.getClass(), check to see if passed object is null.

    /**
     * Test if the given <i>object</i> is equal to me.
     * In this implementation, an <i>object</i> is
     * equal to me iff it has the same runtime
     * type and the <i>name</i> and <i>value</i> attributes
     * are both <tt>equal</tt> (or <tt>==</tt>).
     *
     * @param object the {@link Object} to compare to
     */
    public boolean equals(Object object) {
        if (this == object) {
            return true;
        } else if (null == object) {
            return false;
        } else if (this.getClass().equals(object.getClass())) {
            NameValuePair pair = (NameValuePair) object;
            return ((null == name ? null == pair.name :
name.equals(pair.name))
                   && (null == value ? null == pair.value :
value.equals(pair.value)));
        } else {
            return false;
        }
    }

Thanks,
Rand


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


Re: [lang] NullArgumentException?

Posted by Stephen Colebourne <sc...@btopenworld.com>.
> > I have to reasons for not wanting to use NullPointerException.
> > 1) NullPointerException is an unplanned exception. Its thrown by the
runtime
> > automatically, with no control over the error message. Overloading its
use
> > for specifically validated exceptions is not good.
>
> if(foo == null) {
>   throw new NullPointerException("foo was null");
> }
>
> works fine for me.  I don't see why that's not good.  If it wasn't don't
> you think they would have left off the constructor that took a String
> for the message?

To me, the very name says it all - null *pointer*, not illegal *argument*.

> > 2) The Collection API doesn't allow NullPointerException to be thrown
for a
> > rejection of null entry into a collection. It states it must be
> > IllegalArgumentException.
>
> Collection class documentation:
>
> "Attempting to add an ineligible element throws an unchecked exception,
> typically NullPointerException or ClassCastException."
>
> Collection.add(Object) documentation:
> "NullPointerException - if the specified element is null and this
> collection does not support null elements."

I had check the javadoc too and it wasn't there. I've checked again, and it
was added in J2SE 1.4

> > In both cases NullArgumentException would make life clearer.
>
> I disagree.  It makes distinguishing between two different types of
> exceptional conditions a bit more difficult.  There is frequently a
> difference between passing in a Null and passing in an illegal argument
> and thus two different exceptions should be used (i.e. they shouldn't
> inherit from each other).  If you only want to catch the
> IllegalArgumentException case and let a null based exception propogate
> up the stack, it's not pretty if you have NullArgumentException extend
> IllegalArgumentException.  You'd have to catch the exception, test with
> instanceof and rethrow).

You don't actually need to test for instanceof, an earlier catch clause and
rethrow would do. The point of my argument is that null is an illegal
argument in this case, so should be treated the same way as other illegal
args are. Getting to the stage of having a method call on a null reference
is one stage worse, and that should thus have its own exception, the
appropriately named null pointer.

However, it seems Sun have decided against me. I disagree with that. But
I'll have to live with it :-(

Stephen




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


Re: [lang] NullArgumentException?

Posted by "Michael A. Smith" <ma...@apache.org>.
On Sat, 29 Jun 2002, Stephen Colebourne wrote:
> From: "Michael A. Smith" <ma...@apache.org>
> > On Sat, 29 Jun 2002, Stephen Colebourne wrote:
> > > I would like to add a NullArgumentException class to the lang.exceptions
> > > package.
> > >
> > > It would be a subclass of IllegalArgumentException.
> > > It would be thrown when null is passed in as an argument when it
> shouldn't
> > > be.
> > >
> > > Any objections?
> >
> > That's what java.lang.NullPointerException is for.  "Applications should
> > throw instances of this class to indicate other illegal uses of the null
> > object."
> 
> I have to reasons for not wanting to use NullPointerException.
> 1) NullPointerException is an unplanned exception. Its thrown by the runtime
> automatically, with no control over the error message. Overloading its use
> for specifically validated exceptions is not good.

if(foo == null) { 
  throw new NullPointerException("foo was null");
}

works fine for me.  I don't see why that's not good.  If it wasn't don't 
you think they would have left off the constructor that took a String 
for the message?

> 2) The Collection API doesn't allow NullPointerException to be thrown for a
> rejection of null entry into a collection. It states it must be
> IllegalArgumentException.

Collection class documentation:

"Attempting to add an ineligible element throws an unchecked exception, 
typically NullPointerException or ClassCastException."

Collection.add(Object) documentation:
"NullPointerException - if the specified element is null and this 
collection does not support null elements."

> In both cases NullArgumentException would make life clearer.

I disagree.  It makes distinguishing between two different types of
exceptional conditions a bit more difficult.  There is frequently a
difference between passing in a Null and passing in an illegal argument
and thus two different exceptions should be used (i.e. they shouldn't
inherit from each other).  If you only want to catch the
IllegalArgumentException case and let a null based exception propogate
up the stack, it's not pretty if you have NullArgumentException extend
IllegalArgumentException.  You'd have to catch the exception, test with
instanceof and rethrow).


regards,
michael


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


Re: [lang] NullArgumentException?

Posted by Stephen Colebourne <sc...@btopenworld.com>.
From: "Michael A. Smith" <ma...@apache.org>
> On Sat, 29 Jun 2002, Stephen Colebourne wrote:
> > I would like to add a NullArgumentException class to the lang.exceptions
> > package.
> >
> > It would be a subclass of IllegalArgumentException.
> > It would be thrown when null is passed in as an argument when it
shouldn't
> > be.
> >
> > Any objections?
>
> That's what java.lang.NullPointerException is for.  "Applications should
> throw instances of this class to indicate other illegal uses of the null
> object."

I have to reasons for not wanting to use NullPointerException.
1) NullPointerException is an unplanned exception. Its thrown by the runtime
automatically, with no control over the error message. Overloading its use
for specifically validated exceptions is not good.
2) The Collection API doesn't allow NullPointerException to be thrown for a
rejection of null entry into a collection. It states it must be
IllegalArgumentException.

In both cases NullArgumentException would make life clearer.

Stephen


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


Re: [lang] NullArgumentException?

Posted by "Michael A. Smith" <ma...@apache.org>.
On Sat, 29 Jun 2002, Stephen Colebourne wrote:
> I would like to add a NullArgumentException class to the lang.exceptions
> package.
> 
> It would be a subclass of IllegalArgumentException.
> It would be thrown when null is passed in as an argument when it shouldn't
> be.
> 
> Any objections?

That's what java.lang.NullPointerException is for.  "Applications should
throw instances of this class to indicate other illegal uses of the null
object."

michael


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