[jdom-interest] NoSuch*Exceptions in JDOM
Andre Van Delft
avandelft at epo.org
Wed Jul 12 01:48:37 PDT 2000
You misinterpreted my example.
There is nothing wrong with
str += jnl.getChild("JTL").getContent();
since this uses the exception throwing getChild,
so no NullPointerException can occur.
Getting the other child uses my util function
that may return null; it has a null test:
org.jdom.Element sbt = getChild(jnl, "SBT");
if (sbt != null) str += sbt.getContent();
Nothing can go wrong here.
Please note the line:
// DTD: jnl = jtl,sbt?,...
I meant to say that a JNL contains a JTL and an optional SBT.
Therefore the JTL and SBT children are fetched differently.
Jason Hunter wrote:
> Andre, your example shows exactly what's wrong with returning null from
> getChild()! Notice how you're calling jnl.getChild("JTL").getContent()
> without doing a null check. Well, sometime it's going to happen that
> <JTL/> isn't going to be there, and your only error message is going to
> be a cryptic NullPointerException. That's the reason we chose to throw
> exceptions in the first place; because Brett and I were both tired of
> tracking down NPEs, especially in already released code because this is
> the sort of thing that happens when the end user does something crazy to
> their XML that you didn't anticipate.
>
> -jh-
>
> Andre Van Delft wrote:
> >
> > I am developing a program that transforms XML into HTML, using JDOM and ECS.
> > A typical code snippet is:
> >
> > String str = "";
> > try {
> > // DTD: jnl = jtl,sbt?,...
> > str += jnl.getChild("JTL").getContent();
> > org.jdom.Element sbt = getChild(jnl, "SBT");
> > if (sbt != null) str += sbt.getContent();
> > ...;
> > }
> > catch (NoSuchElementException e)
> > {
> > handle (target, e);
> > }
> >
> > Here getChild(jnl, "SBT") is a call to my util function
> > that returns null instead of throwing an exception.
> > The null test is exactly what I want for an optional element.
> > The catch clause is only handling real exceptions.
> > Without my util function, the code would be far more
> > complicated:
> >
> > String str = "";
> > try {
> > // DTD: jnl = jtl,sbt?,...
> > str += jnl.getChild("JTL").getContent();
> > try {
> > str += jnl.getChild("SBT").getContent();
> > }
> > catch (NoSuchElementException e)
> > {
> > // nothing to do; is OK
> > }
> > ...;
> > }
> > catch (NoSuchElementException e)
> > {
> > handle (target, e);
> > }
> >
> > IMHO: instead of everybody making his own util functions,
> > JDOM should provide the desired function variations.
> >
> > _______________________________________________
> > To control your jdom-interest membership:
> > http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
More information about the jdom-interest
mailing list