[jdom-interest] XMLOutputter & null pointer exception.

Quigley, James P jim.quigley at bankofamerica.com
Wed Aug 16 09:46:21 PDT 2000


Jason, thanks for your prompt reply.

You mentioned 4 cases to consider. I'd prefer returning "" for the first 3.
I'm not sure about the fourth. 
<empty/>
<empty></empty>
setText("")
setText(null)

For the first three cases, the issue is how to represent the absence of
text. This reminds me of the debate over what getChild should return, but
there is a third option. getText can:
1. return a null
2. throw an exception
3. return an empty String

I'm expecting getText to return a String and will use String methods on the
return value. If an empty string is returned, my code can continue to
execute. With either a null or an exception, I have to have additional code
to avoid program termination.

With a null return, I need to use a C style of programming where I check the
return value of every getText before invoking any String method or I get
null pointer exceptions. Any relaxing of vigilance results in debugging null
pointer exceptions, which is what happened with XMLOutputter. Since the
nulls are data dependent, my code can work with some data and then break
when the data changes. In my application, the data comes from another
program that I have no control over.

Exceptions have similar problems in error handling.

Say I'm concatenating the text of several elements into a string buffer

option 1
========
String str;
StringBuffer sb = new StringBuffer();
str = ele1.getText();
if (str != null)
	sb.append(str);
str = ele2.getText();
if (str != null)
	sb.append(str);
 
option 2
========
StringBuffer sb = new StringBuffer();
try {
	sb.append(ele1.getText())
}
catch(NullPointerException ex) {}
try {
	sb.append(ele2.getText())
}
catch(NullPointerException ex) {}

option 3
========
StringBuffer sb = new StringBuffer();
sb.append(ele1.getText())
	.append(ele2.getText());


I'm not sure of the usage of setText(null). If setText(null) is an idiom to
clear the text of an element, I'd prefer a unique method to do so such as
clearText, rather then overload setText. What would setText(null) do for an
element like this <A>aaaa<B>bbb</B>ccccc</A>?



-----Original Message-----
From: Jason Hunter [mailto:jhunter at collab.net]
Sent: Tuesday, August 15, 2000 5:20 PM
To: Quigley, James P.
Cc: jdom-interest at jdom.org
Subject: Re: [jdom-interest] XMLOutputter & null pointer exception.


"Quigley, James P" wrote:
> 
> 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.

In one way it's actually a bug in Element, because a setText("") creates
an empty String content node but then a getText() right after returns
null.  That caused XMLOutputter to get confused when it saw a single
String content node but got the null from getText().

I just did a CVS check-in so 'setText(""); getText()' returns "".  That
at least makes things work in the interim.

Now let's decide what we want long term.  What should getText() and
getMixedContent() return for all these?

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

Having all return null for getText() and empty lists for
getMixedContent() seems like the best choice after 15 seconds of
thinking.  That would mean setText("") would act like setText(null).

-jh-



More information about the jdom-interest mailing list