I know what a ConcurrentModificationException is. I had them before, I solved them before and I can get away with a Iterator. However, In this case I don't understand why it's being thrown.
public boolean pointOnEdgeBlob(int x, int y, float edgeHitEpsilon) {
init();
for (int i = 0; i < nOfBlobs; i++) {
Blob b = blobs.get(i);
// >>>>>>>>>>>>>>>>>>>>> here it calls the method where it goes wrong
if (b.edgeHit(x, y, edgeHitEpsilon)) return true;
}
return false;
}
Here is the edgeHit method that it in the blob:
public boolean edgeHit(float x, float y, float edgeHitEpsilon) {
// quick test if it's worth it
if (x < getMinX()-edgeHitEpsilon || x > getMaxX()+edgeHitEpsilon || y < getMinY()-edgeHitEpsilon || y > getMaxY()+edgeHitEpsilon) {
return false;
}
// the last one should be connected to the first
// >>>>>>>>>>>>>>> if i comment the part in the for loop then this get's the problem.
PVector pre = cornerVectors.get(cornerVectors.size() -1);
PVector cur;
for (int i = 0; i < cornerVectors.size(); i++) {
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> on this line it throws
cur = cornerVectors.get(i);
if(Blob.onLine(pre, cur, x, y, edgeHitEpsilon)) {
return true;
}
pre = cur;
}
return false;
}
Update:
cornerVectors is a list view:
List<PVector> cornerVectors;
It get's set with:
list.subList(fromIndex, toIndex);
There are no other threats running.
Here is the stack trace:
Exception in thread "Animation Thread" java.util.ConcurrentModificationException at java.util.SubList.checkForComodification(AbstractList.java:752) at java.util.SubList.size(AbstractList.java:625) at nl.doekewartena.contour.scanner.Blob.edgeHit(Blob.java:229) at nl.doekewartena.contour.scanner.BlobData.pointOnEdgeBlob(BlobData.java:333) at nl.doekewartena.contour.scanner.ContourFinder.scan(ContourFinder.java:555) at nl.doekewartena.contour.scanner.ContourFinder.scan(ContourFinder.java:469) at exclude.T04_ContourFinder.draw(T04_ContourFinder.java:38) at processing.core.PApplet.handleDraw(PApplet.java:2386) at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240) at processing.core.PApplet.run(PApplet.java:2256) at java.lang.Thread.run(Thread.java:695)
Once you do
x = list.subList(fromIndex, toIndex);
the list should not be modified or it will throw CME when accesing x
From the .subList javadocs:
The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)