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

Re: nullpointer

From: Jim Ley <jim@e-media.co.uk>
Date: Thu, 31 May 2001 10:43:31 -0400 (EDT)
Message-ID: <005d01c0e9df$c2c45740$ca969dc3@emedia.co.uk>
To: "Tom Leuntjens" <tom.leuntjens@bricsnet.com>, "Www-Dom@W3. Org" <www-dom@w3.org>
> since I am getting nullpointer exceptions
> and i thought I am testing it using
>
> if (companyname.item(i).getFirstChild().getNodeValue() != null)
>
> apparantly that is not enough.

Nope, and it never is, this is a simple javascript problem because there
are no children to the item (say) then

companyname.item(i).getFirstChild() evaluates to null.
therefore you have

null.getNodeValue() != null
That is not allowed, null has no methods.

You need to test every single object  before calling a method/accessing a
property.

if (companyname && companyname.item && companyname.item(i) &&
companyname.item(i).getFirstChild() &&
companyname.item(i).getFirstChild().getNodeValue())

Would be what you want to do.  (yes I know it's long...)

Jim.
Received on Monday, 4 June 2001 15:48:15 GMT

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