ControlP5 and "java.util.ConcurrentModificationException"
in
Contributed Library Questions
•
1 year ago
hi,
i'm experiencing a weird problem. i've tried to isolate the code that produce the error and i didn't managed to so i'm going to post quite a lot of code.
i think the problem is that i try to add controllersi (using the methods ControlP5.addToggle() in this example) during an event of the gui and this could bring to some errors in thread and some syncronizations fails..
when i move the slider it should be add a toggle. it is added but with this run time error (silently catched by controlP5):
java.util.ConcurrentModificationExceptionat java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)at java.util.AbstractList$Itr.next(AbstractList.java:343)at controlP5.ControllerGroup.drawControllers(Unknown Source)at controlP5.ControllerGroup.draw(Unknown Source)at controlP5.ControlWindow.draw(Unknown Source)at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at processing.core.PApplet$RegisteredMethods.handle(Unknown Source)at processing.core.PApplet$RegisteredMethods.handle(Unknown Source)at processing.core.PApplet.handleDraw(Unknown Source)at processing.core.PApplet.run(Unknown Source)at java.lang.Thread.run(Thread.java:662)
commenting lines 55-60 the problem disappear.
any ideas are appreciated!
here is the the code:
- import controlP5.*;
- final int MAX_QUAD = 5;
- ControlP5 gui;
- int num_quad = 0;
- int earingLength = 300;
- int k = 0;
- ArrayList<Quad> quads = new ArrayList<Quad>();
- void setup() {
- size(800, 600);
- smooth();
- gui = new ControlP5(this);
- gui.addSlider("setNumQuad")
- .setCaptionLabel("num quad")
- .setPosition(10, 10)
- .setSize(200, 20)
- .setRange(0, MAX_QUAD)
- .setNumberOfTickMarks(MAX_QUAD+1)
- ;
- }
- void draw() {
- background(100);
- pushMatrix();
- strokeWeight(3);
- stroke(0);
- noFill();
- translate(width/2, height/2 - earingLength/2);
- for (Quad q1 : quads) {
- rect(-q1.edgeLength/2, q1.x,
- q1.edgeLength, q1.edgeLength);
- }
- popMatrix();
- }
- void setNumQuad(int n) {
- //println("setNumQuad: "+n);
- if (n==num_quad) {
- // end recursion
- } else if (n>num_quad) {
- //println("add new quad");
- k += 51;
- quads.add(new Quad(k, 20));
- Toggle t = gui.addToggle(""+num_quad)
- .setCaptionLabel("")
- .setPosition(10+num_quad*(200/MAX_QUAD), 40)
- .setSize(10,10)
- .setId(0);
- ;
- num_quad++;
- setNumQuad(n);
- } else if (n<num_quad) {
- k -= 51;
- //println("remove "+n);
- quads.remove(quads.size()-1);
- num_quad--;
- gui.remove(""+num_quad);
- setNumQuad(n);
- }
- //println(quads.size());
- }
- void controlEvent(ControlEvent theEvent) {
- }
- class Quad {
- float edgeLength;
- float x;
- Quad(float x, float e) {
- this.edgeLength = e;
- this.x = x;
- }
- float getDiagonal() {
- return edgeLength; // foo
- }
- float getLastPoint() {
- return x+edgeLength;
- }
- }
1