[jdom-interest] XMLOutputter & null pointer exception.

John Interrante jinterra at nycap.rr.com
Thu Aug 17 19:55:02 PDT 2000


I also encountered the XMLOutputter null pointer exception.  I'd been
calling setText("") to "blank out" an element in a JDOM tree that I
got from parsing an XML document.  Then I'd been calling XMLOutputter
to write out the modified XML document.  I ended up having to do this:

//         collNetXiExt.setText(""); // "" causes a NullPointerException
//                                   // when outputting the JDOM tree as XML
        collNetXiExt.getMixedContent().clear();

Therefore, I want to throw my two cents into this discussion too.
Like everybody else, I'd prefer getText to return "" for each of these
cases,

<empty/>
<empty></empty>
setText("")

Not only would this have avoided the NullPointer exception in
XMLOutputter, it also avoids having to write extra code to check for a
null return value.  I used to write code using Xerces's DOM and
Xalan's XPath API to parse an XML document and extract specific
elements' values as strings.  XPath ALWAYS returned a string, even if
it was "" because the element was empty, and I found that behaviour
very reasonable and easy to work with.  I think Wes Bigg's point about
there being no way, short of metasemantics like a xsl:null="true"
attribute, to represent a null value in XML sums it up.  

setText(null)

This is problematic for another reason besides requiring support for
the xsl:null="true" attribute.  If you read the Javadoc for the Java 2
List container class, you'll find out that it says List doesn't define
the semantics of what happens if you add a null object to a List.
Some List implementations accept it, some don't.

-- 
John Interrante * Niskayuna, NY * GE CR&D, Web & E-Business Solutions




More information about the jdom-interest mailing list