[jdom-interest] How to check for whether an element has children.

Nott, Matthias mnott at vignette.com
Thu Dec 27 05:21:56 PST 2001


Sorry for the newbie question, but why does the following
code result in a null pointer?

	String myXML = 
	Document tXML = new SAXBuilder().build(new StringReader(myXML));
	Element e = tXML.getRootElement();
	Element c = e.getChild("object");
	Element n = c.getChild("object");
	boolean s = n.hasChildren();

If I say, s = c.hasChildren(); I get true, as this is object oid 4712 which
has children oid and name. I would have expected to s = n.hasChildren() to
return false, but not a null pointer. The same error appears if I try to
String s = n.getChildren().toString();

So how can I check without getting an exception for whether an element
still has children? I can convert it to a list and then check out the
index for the <object> element, but I am a bit afraid I might be loosing
performance here.



More information about the jdom-interest mailing list