W3C home > Mailing lists > Public > www-dom@w3.org > April to June 2001

Re: nullpointer

From: Joseph Kesselman <keshlam@us.ibm.com>
Date: Thu, 31 May 2001 09:24:40 -0400
To: <www-dom@w3.org>
Message-ID: <OFC874DF9C.4119ADCA-ON85256A5D.00479F8E@pok.ibm.com>

>   if (companyname.item(i).getFirstChild().getNodeValue() != null) {

Are you sure companyname.item() isn't null?
Are you sure companyname.item().getFirstChild() isn't null?

You should probably test those before testing companyname.item
().getFirstChild().getNodeValue().


>var x = document.createRange();
>x.setStart(document.getElementsByTagName("p")[0],0);
>x.setEnd(document.getElementsByTagName("p")[0],1);
>x.insertNode(document.getElementsByTagName("div")[0]);
>
> Suppose that the document does not contain any div elements.
> What exception should be thrown?

Work it through.

The use of [0] to access a NodeList in ECMAScript is officially defined as
equivalent to the item() method.

getElementsByTagName() will return an empty nodelist if it doesn't find
anything. So your first risk is that there are no <p> elements. If that's
true, the item(0)/[0] returns null. The definition of Range.setStart and
Range.setEnd says this parameter MUST NOT be null. We don't actually say
what exception gets thrown if it is. That may or may not be an erratum. I
take it to mean that a system-specific null pointer error will be thrown
eventually. Note _eventually_; it may be discovered only when you try to
access the mis-configured Range. But one could certainly justify
HIERARCHY_REQUEST_ERR (null is never a legal child) or
INVALID_NODE_TYPE_ERR.

Assuming the first <p> is found -- and note that you are only checking that
one! -- you search for a <div>. (You aren't searching within that <p>, but
anywhere within the document... which seems odd, but OK.) You may not find
it; in that case you're trying to insert null into the range. That's a
nonsensical operation, and we should throw an exception. Again, we don't
specify which. I would expect a null pointer exception, but one could
certainly justify HIERARCHY_REQUEST_ERR (null is never a legal child) or
INVALID_NODE_TYPE_ERR.


This is definitely in the range of "If it hurts when you do that, stop
doing that."

______________________________________
Joe Kesselman  / IBM Research
Received on Thursday, 31 May 2001 09:25:36 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:13:48 GMT