• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking if ResultSet is empty

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I KNow how to check if a ResultSet is empty but for some reason it keeps telling me my ResultSet is empty even though i know it isn't.
As part of a program i am developing, a query is executed, i then get that ResultSet. So far i have just been sending it to another JFrame that displays it in a JTable. I cant guarantee if the ResultSet actually has results or not so i have set up method which checks this and returns a boolean if its true or not. The problem is it always returns false even though i know its not.
I dont understand how checking the result set returns a false indicating its empty, but if i send it directing to TableResultsGUI it will display fine . .

The bit of code calling my method


My method to check the ResultSet
 
Stevens Miller
Bartender
Posts: 1377
28
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it matter that you are passing rs to isEmpty(), but rsArray to TableResultsGUI()?
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't forget that after you've read the first record to see if the ResultSet is empty, you've moved the pointer to the second record. That means that when you try to read the data from the non-empty ResultSet you'll miss the first record.

I would suggest not trying to find out in advance whether the ResultSet is empty. Go ahead and produce the table model (or whatever you're doing) from the ResultSet, and then if the table model has zero rows then you know the ResultSet was empty. If not, then carry on and use it.
 
Stevens Miller
Bartender
Posts: 1377
28
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:And don't forget that after you've read the first record to see if the ResultSet is empty, you've moved the pointer to the second record. That means that when you try to read the data from the non-empty ResultSet you'll miss the first record.

Are you sure about that, Paul? The javadoc suggests the first call moves it to the first record:
the API programmers wrote:A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you're correct. And if you then wrote code which knew you already called rs.next() once and that it had returned true, that would be fine. But most likely you're then going to write another method which uses the standard "while (rs.next())" idiom, and that will miss the first record.
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact there is rs and rsArray will not make a difference now. There are two result sets that could be passed to TableResultsGUI so i am doing a check on the first ResultSet rs.

Is there not a way of perhaps creating a new array from the one i want to test, then doing a check on that to determine if its empty or not? Seems to much extra work to do though. The problem i have with producing the table model is it gives me null pointer exceptions so i would rather i dealt with the issue before it got that far.
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tom davies wrote:The problem i have with producing the table model is it gives me null pointer exceptions so i would rather i dealt with the issue before it got that far.


Then you're doing it wrong. It should be extremely simple to create a table model and then put all of the records from the ResultSet into it. If there are no records, that shouldn't be a problem.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12266
36
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tom davies wrote:
The bit of code calling my method


Unless there is more code here, your results variable immediately goes out of scope and is lost forever... That may not be the case if there is more code you didn't post (which is OK).
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
tom davies wrote:The problem i have with producing the table model is it gives me null pointer exceptions so i would rather i dealt with the issue before it got that far.


Then you're doing it wrong. It should be extremely simple to create a table model and then put all of the records from the ResultSet into it. If there are no records, that shouldn't be a problem.


Hi Paul, below i have added the code which creates my table model and also the error that i get.



Example error, Line 260 in TableResultsGUI being line 12 in the above code.


Fred i am not sure what you mean by my results variable going out of scope, but there is indeed more than that code snippet, that is just the only addition to my working code so that i could call the method i mentioned above.
 
Stevens Miller
Bartender
Posts: 1377
28
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Yes, you're correct. And if you then wrote code which knew you already called rs.next() once and that it had returned true, that would be fine. But most likely you're then going to write another method which uses the standard "while (rs.next())" idiom, and that will miss the first record.

Oh, agreed, completely. The code he's provided does seem to risk a classic "one-ahead" mistake. But, as written, it does test for an empty set and, when the set's not empty, leaves his cursor at the first record after the test. Lots of better ways to do it, but I did want to be clear about the reason this one is problematic.
 
Stevens Miller
Bartender
Posts: 1377
28
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tom davies wrote:Example error, Line 260 in TableResultsGUI being line 12 in the above code.

Sure seems to me that this means your constructor is being passed null. Can you show us the code that obtains the ResultSet you pass at line 75 of TableResultsGUI.java?

Fred i am not sure what you mean by my results variable going out of scope, but there is indeed more than that code snippet, that is just the only addition to my working code so that i could call the method i mentioned above.

He means that "empty" will cease to exist at some point, most likely when the method that declares it at Line 01 of your original snippet returns. I'm not sure why that's important, though; your problem wouldn't change if you eliminated that variable entirely and ran this instead:

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 75 is line 03 below. I pass the ResultSet as a parameter into TableResultsGUI and store the ResultSet as one of my instance variables through the constructor.
Then when i create the JTable i call the set model method below. This all works fine with correct ResultSets just not ones yielding no results.

 
Stevens Miller
Bartender
Posts: 1377
28
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well... we need to see how your ResultSet is defined. Might as well go ahead and post your whole program.
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:Well... we need to see how your ResultSet is defined. Might as well go ahead and post your whole program.


I think i may be getting the NullPointerException due to my SQL queries. Using my other searches i can enter details which will yield no results and i get the blank table which is what i want, but other searches i get the error. I will make some changes to the queries and see if that works.
 
Stevens Miller
Bartender
Posts: 1377
28
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense. The thing about a null-pointer exception is that, well... it means you've got a null pointer. In your situation, you need to find out why that's the case, not cope with it after you get one.

Good luck.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic