[jdom-interest] XMLOutputter & null pointer exception.

Quigley, James P jim.quigley at bankofamerica.com
Tue Aug 15 15:43:51 PDT 2000


I've been using JDOM for several weeks now and it makes using XML very easy.
Thanks to all who have worked on it.

I recently started trimming some elements in documents I'm building with
JDOM. In some cases, I'll have an empty string. 
XMLOutputter now fails with a null pointer. I looked at the source in cvs
today & this problem still exists.

I tracked down my problem to this line in XMLOutputter; getText can return
null & escapeElementEntities throws a null pointer exception.
401  out.write(escapeElementEntities(element.getText()));

The quick fix was to check for null before passing data to
escapeElementEntities. I used the conditional operator for 
minimal code changes, using a local variable would avoid calling the
function twice.
     out.write(escapeElementEntities(element.getText() == null ? "" :
element.getText()));


The larger issue is whether getText should return null for an empty String.
I looked at getTextTrim and it will 
return an empty String. I'd expect the two methods to return identical
values for an empty string. My preference 
would be the empty string to avoid pesky null pointer exceptions.

Also, perhaps escapeElementEntities should check for null parameter.

I'm not sure of the protocol for identifying and fixing errors, so I'm
sending this note to the interest list.

TIA



More information about the jdom-interest mailing list