Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic content in SQLite query and error because it's not "final"  RSS feed

 
Matt Novak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to execute SQLite query like this:



The problem is:
error: local variables referenced from a lambda expression must be final or effectively final stmt.executeUpdate(Q);

How can I generate query command using conditionals and make it work? :-/
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not showing all the relevant code. I don't see the lambda expression that the compiler is complaining about. Apparently the stmt.executeUpdate(Q); is in a lambda expression, but you didn't post that. You're also not showing how the variable stmt is declared. Please show us the complete code, otherwise it will be a lot harder to explain exactly what's wrong.

Variables used inside a lambda expression must be effectively final. Apparently your variable stmt is not, that's why you get an error. You can try making it final: final Statement stmt = ...;

Note also: The = new String(); is not necessary at all. You're going to set gender to either "1" or "0" in the following if-statement - throwing away the empty String object you just created.
 
Matt Novak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code with lambda expression... it's an event that happens when users click save button.
Stmt has been defined before this expression in same function:


and is working great when selecting data above this event:

 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's almost never a good reason to say new String(). Take them out of 4 and 13 and try again.
 
Matt Novak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, now I added a function:



and did the event:



Had to add final to "Statement stmt = ...".

I don't really get it why it wouldn't work before... :-/
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:There's almost never a good reason to say new String().

@Matt: Indeed, I wonder if you need a String at all, except to provide a value for your database table (and why that would be "0" or "1", as opposed to the more usual "M" or "F", I have no idea). What about:It might not help with your database stuff, but it sure makes more sense to me than using a String as a "gender". You might also want to read the StringsAreBad page for more details.

HIH

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!