aspose file tools*
The moose likes JDBC and the fly likes rs.next() check for empty recordset gives nullpointer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "rs.next() check for empty recordset gives nullpointer" Watch "rs.next() check for empty recordset gives nullpointer" New topic
Author

rs.next() check for empty recordset gives nullpointer

Mike McMahon
Ranch Hand

Joined: May 07, 2005
Posts: 30
Hey Folks,
I'm trying to handle cases where a query results in no results. Instead of an error page, I'd like a friendly message.

Here's what I have:


I found this snippet out on the net, and it looks simple, but I'm getting a nullpointerexception on the line with if (rsStns.next()) {

Am I missing something obvious?

Cheers,
Mike
rogel garcia
Ranch Hand

Joined: Dec 19, 2003
Posts: 41
Can you post the stack trace?


SCJP 1.4
Mike McMahon
Ranch Hand

Joined: May 07, 2005
Posts: 30
Alas - My sql was botched. I ran it directly (through iSQLPlus) and found an error. I guess Java was right, and i was pointing at null!
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
It looks like rsStns is null, which means executeQuery returns null...

I would have hoped that executeQuery either returned some
results (even if empty) or threw an exception though.

The stack trace would be great as rogel suggests.

Have you tried using a simpler sql question?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39433
    
  28
NullPointerExceptions are usually a beginning topic, but if there is an error in the SQL it might be better to describe the SQL in our databases forum, where I am moving this thread.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: rs.next() check for empty recordset gives nullpointer