• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.sql.SQLException: Operation not allowed after ResultSet closed

 
Vani Sweety
Ranch Hand
Posts: 79
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have written this piece of code and am getting an error like:
java.sql.SQLException: Operation not allowed after ResultSet closed
How do i fix this please

 
Les Morgan
Rancher
Posts: 710
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a line number with that exception?

java.sql.SQLException: Operation not allowed after ResultSet closed
 
Vani Sweety
Ranch Hand
Posts: 79
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no
but if i put only one statement like this:

the codes works
the problems crops up when i insert the 2nd statement
 
Liutauras Vilda
Bartender
Pie
Posts: 2762
111
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Also, why are you using floats on lines: 22, 23 and 25?
2. Variables using not Java convention. It is more like Python. In Java variables should start with lower case. Next word should start with an upper case. It should read totalSales, totalBuying (not sure if this name is good in general).
3. Also you're missing multiple curly braces around if-else statements (lines 26, 28). Don't omit them.

I suspect you have stmt defined as an instance variable and you're trying to use it later outside this method when you close it on line 31.
 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation for Statement says:

By default, only one ResultSet object per Statement object can be open at the same time.


So if you need two ResultSets open at the same time (which you don't really need but it might be more convenient) then you'll need two Statements.
 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also you're going to need to call rs2.next() to read the record from the second query. And why aren't you using ResultSet's getFloat() method, instead of doing a float-to-String-to-float conversion?
 
Vani Sweety
Ranch Hand
Posts: 79
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have tried correcting it this way
am getting this error:

java.lang.NullPointerException

 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't just read the error message. Look at the stack trace which contains it, there's a lot of information there. If you don't know how to extract useful information from a stack trace, post it here.
 
Vani Sweety
Ranch Hand
Posts: 79
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is all the code for that part
there is no stack trace....
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Don't do this in the catch block, do this:

so you get the stack trace and not just the exception message.
 
Vani Sweety
Ranch Hand
Posts: 79
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or i can put these two values in a table then using only one sql toretireve back the information
 
Vani Sweety
Ranch Hand
Posts: 79
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here are the errors :

java.lang.NullPointerException
at CashierMenu$4.actionPerformed(CashierMenu.java:161)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
 
Vani Sweety
Ranch Hand
Posts: 79
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and at the line 161
at CashierMenu$4.actionPerformed(CashierMenu.java:161)
i have the code for the 2nd statement
ResultSet rs2=stmt1.executeQuery(sql1);
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where in your code do you initialise stmt1, since at the point you are using it it is null?
 
Vani Sweety
Ranch Hand
Posts: 79
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hereis where i have initialsie them
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's declaring it, not initialising it.
Somewhere you are doing stmt = con.createStatement(), so where are you doing stmt1 = con.createStatement()?
 
Vani Sweety
Ranch Hand
Posts: 79
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
Somewhere you are doing stmt = con.createStatement(), so where are you doing stmt1 = con.createStatement()?

i have omitted this part
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that fix the problem then?
 
Vani Sweety
Ranch Hand
Posts: 79
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes now its working
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic