OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.


Help: OASIS Mailing Lists Help | MarkMail Help



   RE: [xml-dev] skipping the null node values in a DOM Tree

[ Lists Home | Date Index | Thread Index ]

>if (!chhh.getFirstChild().getNodeValue().equals(null))

Here you are testing whether the string value of the first child node of chh is equal to null. No string in Java ever equals(null) - String.equals(object) only ever returns true if the object is an instanceof String, and null is not an instanceof anything.

If chhh has no children, such as the <description/> element in your example, there is no first child, and getFirstChild() will return null [1]. Attempting to call getNodeValue() on that null reference will cause a NullPointerException to be thrown, which you are observing.

If you are using Java 1.4, use (chhh.getFirstChild() != null) or (chhh.getChildCount() > 0) before walking down the tree accessing the text (and remember that if your source document has not been normalised or contains comments or processing instructions the text content of the element may be spread across many nodes, and the node value of the first node may not be text content).

If you are using Java5 then calling getTextContent() [2] on chhh hides these complications.



1:  http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/Node.html#getFirstChild() 

2: http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Node.html#getTextContent()

This email and any files attached are intended for the addressee and may contain information of a confidential nature. If you are not the intended recipient, be aware that this email was sent to you in error and you should not disclose, distribute, print, copy or make other use of this email or its attachments. Such actions, in fact, may be unlawful. In compliance with the various Regulations and Acts, General Dynamics UK Limited reserves the right to monitor (and examine for viruses) all emails and email attachments, both inbound and outbound. Email communications and their attachments may not be secure or error- or virus-free and the company does not accept liability or responsibility for such matters or the consequences thereof. Registered Office: 100 New Bridge Street, London EC4V 6JA. Registered in England and Wales No: 1911653.


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS