• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't pin down this NullPointerException :(  RSS feed

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line of code:



Is causing a NullPointer exception for some reason and I can't pinpoint the cause.

The full class looks like this, and all the other methods, including " public boolean nodeExists(String expr, Object item) {" work fine and are actively being used.

Here's the entire class source (I'd have cherry picked the vital bits if I had an idea what the cause was):



Here's the usage of it (from a factory class building an object from an XML schema):



I'm lost and I'm sure this is something trivial. I'm sure I'm not doing myself any favours by offering "null" as a return value from the XmlReader class methods really so I'll change that too.
 
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


JavaDocs for XPath say that if expr, or the input source are null, you get a NullPointerException. If this was my problem, I would do the getInputSource in a separate statement and check the value for null and would also check the expr for null.

Bill
Bill
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll make that change too, but I think I also realised (my inexperience showing here) that although non primitive types are Nullable, you can't return "null" from a method. Is this correct? The compiler won't tell you anything is wrong, but at runtime you get these NullPointerExceptions.

Changing my code to throw exception rather than allowing a null return value revealed that an XPathExpression was failing to evaluate, which could (read, will) be down to one of the two things you've mentioned

Thanks for the tips
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot to mention. These forums feel so much more welcoming than the ones over at Sun
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Corbyn:
...although non primitive types are Nullable, you can't return "null" from a method. Is this correct?

I didn't read the whole discussion above, but no, that is not correct; you can certainly return null from a method - except if the return type of the method is a primitive type.
[ August 16, 2007: Message edited by: Jesper Young ]
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:

I didn't read the whole discussion above, but no, that is not correct; you can certainly return null from a method - except if the return type of the method is a primitive type.

[ August 16, 2007: Message edited by: Jesper Young ]


Ah, of course, but the problem is down to me not checking if getInputSource() is NULL before I then try passing it to evaluate(). This is clear now.

Thanks heaps guys
 
Chris Corbyn
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to put closure to this case. InputSource wasn't null, but it was causing the NullPointerException elsewhere. Once data had been read from the InputSource for the first time it prevented any further reading of data (I guess the entire stream gets read) which the XPath stuff didn't like. Using org.w3c.dom.DocumentBuilder in order to turn the InputSource into a complete DOM tree first solved the problem perfectly.

Erm, EDIT | That would be javax.xml.parsers.DocumentBuilder sorry. My bad.
[ August 17, 2007: Message edited by: Chris Corbyn ]
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Corbyn:
Forgot to mention. These forums feel so much more welcoming than the ones over at Sun


I too agree to this fact but not at the cost of speaking ill about other community but in an appreciating-and-agreeing manner of this forum.

Perhaps, it would be because of people and their interest in visiting the forums could directly contribute i guess!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!