I have 2 clients connected to a server, they send new coordinates or spawn new circles and the server updates the position of the circles or creates new ones and draw them. I use a timertask with a delay of 5 seconds and a 5 seconds interval to spawn new circles.
I keep getting IndexOutOfBoundsException at random indexes every time i run the application when i setTranslateX.
private void updateSimulation(ArrayList<String> cl) {
if(cl == translatedCoorsMessage) {
Platform.runLater(() -> {
int x= 1;
for(Circle circle : circlesOnCorridorOfClient) {
circle.setTranslateX(((Double.parseDouble(cl.get(x))) - 20.0)/1000);
x=x+2;
}
circlesOnCorridorOfClient.addAll(newCirclesToAddOnCorridor);
newCirclesToAddOnCorridor.clear();
});
}
else {
Platform.runLater(() -> {
int x= 1;
for(Circle circle : circlesOnCorridorOfClient1) {
circle.setTranslateX(((Double.parseDouble(cl.get(x))) - 967.0)/1000);
x=x+2;
}
circlesOnCorridorOfClient1.addAll(newCirclesToAddOnCorridor1);
newCirclesToAddOnCorridor1.clear();
});
}
}
This error can happen after the second circle spawns or third or the 5th, etc. I set my x = 1 because the first element in the ArrayList i pass to this method is just a small String to know what i will do with the ArrayList. In this case, update. the second is X coordinate and third is Y. It feels like the foreach loop runs one extra time some times after i add a new circle. I only add new circles after the loop and i do it through another arraylist.
Any tips will be apreciated. thanks for your time.