• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

rs.next and local variables

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using the rs.next comand in  a program and very  well. But when I try to  use in  a jbuttomn throws an error" local variables referenced from an inner class must be final or effectively final"
   How can I fix this?


 
Marshal
Posts: 65443
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Thank you for using the code button, but you didn't quite have it right. This link tells you about the code button, but since you are new I have corrected the error for you and doesn't it look better Unfortunately your indentation is inconsistent, and yo uhave many lines too long. Remember who will have the most difficulty if you write obscure code: you yourself.
Please give us more details about the compiler error, including which line it occurs on. Yes, a local variable referred to from an anonymous class must not be reassigned to. In Java7 you had to label all such variables final, but the Java8+ compiler can check the variables for reassignment.

Don't try to squeeze everything into one class, even less into one method.
 
Bartender
Posts: 6132
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are declaring 'rs' inside the main() method but you're attempting to access it inside an anonymous inner class button2.addActionListener( new ActionListener(){...}. If you want a variable to be visible between methods you can make them instance variables or pass them as parameters. I prefer to make a non-anonymous class extending AbstractAction. This allows you to pass a parameter. However, this is made difficult (impossible?) because you've put everything inside main() and you don't have an instance of JTableResultSet to operate on because main() is static.

See my signature for a link to MainIsAPain.
 
Carey Brown
Bartender
Posts: 6132
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, you'll need this as 'a' way of doing it.
Change the addActionListener to this, doing away with anonymous and allowing parameters.
Then, after main() create the Button2Acation class.
...although, I'd process the 'rs' to get the CellRect and pass that in instead of the 'rs' itself. !!! Don't put everything in main() !!!
 
Carey Brown
Bartender
Posts: 6132
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Campbell already said, your indenting makes reading your code difficult. Here's what it looks like after running it through an Eclipse formatter.
 
Victor Deval
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello:
    Thanks for your reply I did exactly as you tell a it run OK. But rs is empty. What I'm doing wrong. I'm new at this but I'm not giving up.

              Thank you
 
Carey Brown
Bartender
Posts: 6132
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 60 is the last place you use 'rs' and it's inside a loop that doesn't finish until 'rs' has been entirely read.
 
Carey Brown
Bartender
Posts: 6132
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 210 'rs' has reached the end so next() doesn't do anything.
Lines 211 and 212 don't make use of 'rs' even if there was data.
 
Carey Brown
Bartender
Posts: 6132
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have the right approach on line 66 & 67 of closing the ResultSet and the Statement but you've commented them out. In the mean time you've created a Vector of rows with all the data from the ResultSet, perhaps that is what you should be passing to Button2Action (?). I'm not quite sure what your intent is here.
 
Carey Brown
Bartender
Posts: 6132
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

button1-6 are lousy names. How about...
topButton, nextButton, backButton, ...
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!