What causes this exception? How can we prevent this exception? How to fix it?
Example:
I have an ArrayList where I keep several movies in a table (Jtable) where I remove the movies so I do not have them and I have a method to remove a movie from Arraylist, that is, from the table. When I remove a movie leaving a movie in the table, this exception does not occur, but when I remove a movie without having any movie in the table this exception occurs.
Method to remove movies:
private void removeFilmeNaLista(){
for (Iterator<Filme> it = listaFilmes.iterator(); it.hasNext();) {
Filme i = it.next();
// aqui eu removo
listaFilmes.remove(i);
System.out.println("remove:" +(i));
refazTabela();
}
}
Error:
remove:model.Filme@16e0921
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at view.VideoLocacao.removeFilmeNaLista(VideoLocacao.java:495)
at view.VideoLocacao.btnExcluirTbl2ActionPerformed(VideoLocacao.java:392)
The error appears from this line 495 where that is 'File i = it.next ();'.