[jdom-interest] NullPointerException... maybe change it?

bob mcwhirter bob at werken.com
Sun Jan 20 13:50:02 PST 2002

> An attempt to get an Element with xyz.getChild("Name") when
> the Element "Name" is not present in the XML document
> results in a NullPointerException (ie, if tag Name is
> optional).

You sure that's what's going on?

Methinks that you're getting the NPE because 'xyz' is null,
and you're attempting to call getChild(...) on it.

xyz.getChild("name"), if 'xyz' is not null, will simply
return a null.  It won't throw an NPE, last I heard.

> You might also consider a quick "search" method for the
> Element name without having to traverse the nodes in
> sequence:
> public boolean isElementPresent(Name){}
> This allows one to quickly check to see if Elements are
> present and to avoid any getChild(Name) method calls if the
> element Name is not in the document.

If it's present, you probably want to do something with it:

	Element bar = foo.getChild("bar");

	if ( bar == null )
		// nope, no bar here
		// frobnicate bar


More information about the jdom-interest mailing list