This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes getSingleResult returns NPE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "getSingleResult returns NPE" Watch "getSingleResult returns NPE" New topic
Author

getSingleResult returns NPE

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have a query and it can return nulls for each row:



Unfortunately, the program interrupts the methods and returns a NullPointerException, when myQuery returns null. How can I handle that? You can see, I have a NoResultException which also returns null, when no results are found. But it does not prevent from causing a NPE.

I know, I can catch it with catch (NullPointerException ex), but what is the sense of NoResultException, when it cannot catch the NullPointerException implicitly??

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Just don't chain your method calls. e.g:




JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
 
subject: getSingleResult returns NPE