• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

parsing an empty tag

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have inherited some code that parses an XML document. I don't know XML very well, and I've certainly not used Java to do it. That's my disclaimer.

So my program is dying, with what I assume is an NPE - the log says:


the line in question is as follows:


The XML is wellformed and valid, according to the schema. However, the tag for this field are empty... in other words, i have this in the XML:

<the_string></the_string>

Am i correct that when we try and get this, we getNodeValue(), we get a null object. Then, we try and call trim(), causing the NPE?

We are using the org.w3c.dom.Document/Element/Node/etc. classes to do this, if that make any difference.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. The getNodeValue() method always returns null for an element, regardless of whether it's an empty element (the correct XML terminology for what you're calling an empty tag) or not. More likely you're calling getFirstChild() on an empty element and getting null, then proceeding to call getNodeValue() without checking that.
 
Fritz Urling
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so the nodes.item(i) is returning the empty element - the nothing inside the <my_string> tag. then the getFirstChild() blows up.

However, when we HAVE a value in there (i.e. we do NOT have an empty element), we get the element. getFirstChild would then return the string, and life is good again...

I don't know if this is considered a good fix, but we ended up changing the schema. while the <my_string> tag has always had a 0...n restriction, I changed the minLength restriction to 1. they can now not send the string if they don't want to (just like before), but if they include it, there has to be at least one character in it.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Personally I would NEVER write code like that. I would use separate statements to see if getFirstChild() returned a Node or null and I would examine the result of getNodeValue() to see if it was null before executing trim(). On catching these errors you might supply a default "" for the String value and log the problem.

There are so many assumptions built into that line that it is like a time-bomb in your code - just waiting to explode. Changing the schema is NOT a good fix.

Remember <thing></thing> and <thing/> are equivilent as far as XML legality is concerned but in one case <thing> has an empty child Node and in the other it does not.
Bill
[ December 15, 2006: Message edited by: William Brogden ]
 
Fritz Urling
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also think chaining method calls like that is a bad idea. Unfortunatly, this code is littered with lines like that. I"m not sure the Powers That Be will let me go in and start hacking away at them, when the current code "works".

the schema change was the quick and dirty fix (it fixed the immediate problem), and unfortunatly, now that it's in... well, you know the drill. We don't have time to do it right, but we have time to do it over and over and over...

maybe in my down time i can start playing with it and try and get it working better...

Thanks!
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic