Patrik Horváth Patrik Horváth - 14 days ago 5
Java Question

Local X from FOR to event

i have problem here :

w = st.getREFkartaFalse(x).getHodnotaKarty();

Error :
local variables referenced from a lambda expression must be final or effectively final


is there any way take value from local x into event ?

else if (st.getHracTrueAleboHracFalse() == false) {
for (int x = 0; x < st.getHracFalse().size(); x++) {
getChildren().remove(st.getKartaHracFalse(x));
}
st.zoberKartuFalse();
for (int x = 0; x < st.getHracFalse().size(); x++) {
final int offsetx = 80;
st.getREFkartaFalse(x).setTaleboF(false);
st.getKartaHracFalse(x).setTranslateX((400 + (offsetx * x)));
st.getKartaHracFalse(x).setTranslateY((780));
getChildren().addAll(st.getKartaHracFalse(x));
st.setPomPocitadloFalse(x);
st.getKartaHracFalse(x).setOnMousePressed(eventUI -> {
int q;
int w;
q = st.getArrayListkartyArrayList().size();
w = st.getREFkartaFalse(x).getHodnotaKarty();
if (w == q) {

}
});
}
st.setHracTrueAleboHracFalse(true);
}

Answer

Yes. You have to capture the value of x in a local (implicitly) final variable:

...
st.setPomPocitadloFalse(x);
int y = x;  // <=========================== Capture value of x
st.getKartaHracFalse(x).setOnMousePressed(eventUI -> {
     int q;
     int w;
     q = st.getArrayListkartyArrayList().size();
     w = st.getREFkartaFalse(y).getHodnotaKarty(); // <========  use here
     if (w == q) {

     }
 });