Issue
I have this code, lista is an ArrayList of Points that i add to the list everytime the user of the interface drags the mouse. But when i do that i get an error: "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 3"
for(int i=0;i<lista.size();i++){
g.fillOval(lista.get(i).x,lista.get(i).y,radio*2,radio*2);
if(lista.size()>1){
g.drawLine(lista.get(i-1).x,lista.get(i-1).y,lista.get(i-2).x,lista.get(i-2).y);
}
}
How can I fix this?
Solution
On your first iteration through this loop, i-1 will be -1 (and i-2 will be -2) this is where you problem is
for(int i=0;i<lista.size();i++){
g.fillOval(lista.get(i).x,lista.get(i).y,radio*2,radio*2);
if(lista.size()>1){
g.drawLine(lista.get(i-1).x,lista.get(i-1).y,lista.get(i-2).x,lista.get(i-2).y);
}
}
Answered By - ControlAltDel
Answer Checked By - Mary Flores (JavaFixing Volunteer)