This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Reading NULL xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Reading NULL xml" Watch "Reading NULL xml" New topic

Reading NULL xml

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 140
I have an XML which looks like this:

And while I was using to code below to read in "<description/>", I get a null pointer exception because it is not "<description></description>".

How can I allow it to read "<description/>" as ""?

I am calling readTwoTags as readTwoTags("root", "title", "description", myXML);

Thanks a lot.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14961

In which line do you get the NullPointerException? The stack trace tells you exactly where the exception happens.

I'm guessing it is in line 32:

When you have "<description/>", that tag does not have any child tags, so el.getFirstChild() returns null. When you try to call a method (getNodeValue() in this case) on a reference that is null, you get a NullPointerException.

Put the result of el.getFirstChild() in a variable, and check if it is null before calling getNodeValue() on it.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 140
Thanks Jesper. It worked.
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 140
Another question:

How do I read in the value 3 and 4 under key of duration?:

Pleas help. Thanks.
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 140
Anyone, please?
Steve Fahlbusch

Joined: Sep 18, 2000
Posts: 595

Please be patient - all of the moderators are volunteers and they do an amazing job.

so step back and wait
Paul Clapham

Joined: Oct 14, 2005
Posts: 19728

Well, those things aren't "under" that "key" at all. At least not in the normal sense where you think of an XML document as a tree structure -- they are in the next branch of the tree. So it's best not to ask the question that way, even if in that text representation of the XML they do appear to be "under" the <key> element.

If you're writing code which navigates through that document via a tree structure (like a DOM for example), and you're looking at a <key> element, then go up to its parent, find the <value> element which is its child, and then find its child which will be a text node.
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 140

I have tried:

But I can only print out "key duration". The next sibiling value: is null.

Please help. Thanks
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13006
Looks to me like the nextSibling() is NOT the "value" element but a Node of type Text containing non-printing characters.

g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 595
You sure can try to understand the reason why it is done that way, in particular by displaying the getNodeType() of each nextSibiling [sic] during the looping.
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 140
Thanks! It works.
I agree. Here's the link:
subject: Reading NULL xml
It's not a secret anymore!