# Clearing an ArrayList

**URL:** <https://discourse.processing.org/t/clearing-an-arraylist/3697>\
**Category:** Libraries\
**Created:** [September 20, 2018, 3:04pm UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697 "2018-09-20T15:04:11Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![eduzal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eduzal/32/759_2.png) [@eduzal](https://discourse.processing.org/u/eduzal)\
**Post date:** [September 20, 2018, 3:04pm UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697/1 "2018-09-20T15:04:11Z")

</div>

I’ve around this issue a few times. I have a particle system that contains an ArrayList of particles. I use a separate window for the interface programmed using Control P5.

usually my particle system lies under a boolean, and that boolean is activated/deactivated by a cp5 toggle. every time i toggle it to false, i want the ArrayList inside the particle system to be cleared, so the particle system starts fresh from zero instead of a simple interruption it’s runtime.

so i put a ps.particles.clear(); in the toggle false condition. everytime it crashes and I get IndexOutOfBoundsException. it also happened to me with a bang and a button.

what is the most efficient way to really clear an ArrayList?

here is the code with whats going on.

main code

```auto
import controlP5.*;

ControlFrame cf;
RingSystem r;

//RING
boolean isRing=false;
PVector cPos;

void settings() {
  size(1024, 1024, P3D);
}

void setup() {
  //CONTROLP5
  cf =new ControlFrame(this, 400, displayHeight, "MENU", width, height);
 //RING SETUP
  cPos=new PVector(width/2, height/2, 0);
  r=new RingSystem(cPos);
}

void draw(){
  if (isRing) {
    if (frameCount%30==0) r.addRing();
    r.display();
  }
}

```

Ring System

```auto
class RingSystem{
 ArrayList<Ring> rings;
 PVector origin;
 RingSystem(PVector _origin){
   rings=new ArrayList<Ring>();
   origin=_origin.copy();
 }
 
 void addRing(){
  rings.add(new Ring(origin)); 
 }
 
 void display(){
   for(int i=rings.size()-1; i>0; i--){
    Ring r=rings.get(i);
    r.update();
    r.display();
    if(r.isDead()){
     rings.remove(i); 
    }
   }
 }
}

```

Control Frame

```auto
    cp5.addToggle("ringToggle")
      .setPosition(160, 50)
      .setSize(80, 80)
      .setValue(false)
      ;
  void ringToggle(boolean toggle) {
    if (toggle==true) {
      isRing=true;
    } else {
      isRing=false;
      
      //r.rings.clear();
      for (int i=0; i<r.rings.size(); i++) {
        Ring ri=r.rings.get(i);
        r.rings.remove(i);
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [September 20, 2018, 4:16pm UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697/2 "2018-09-20T16:16:30Z")

</div>

In the `ringToggle(boolean)` method use `r.rings.clear()` it will _not_ throw an IndexOutOfBoundsException exception, that is from somewhere else.

I suspect that the exception is thrown from the `display()` method.

Also I think the loop condition should be `i > = 0` otherwise the first ring will never be updated.

Look at the exception stack trace and follow it down, can you see the method where the exception is thrown, if not copy and paste the trace here.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 20, 2018, 4:19pm UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697/3 "2018-09-20T16:19:57Z")

</div>

- [Forum.Processing.org/two/discussion/28120/clearing-an-arraylist](http://Forum.Processing.org/two/discussion/28120/clearing-an-arraylist)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 20, 2018, 4:23pm UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697/4 "2018-09-20T16:23:00Z")

</div>

I also assumed that `display()` is causing this.

You could set a `cleared` boolean to `true` and in `display()` **within** the for loop check it and on true leave with `return;`

---

<div class="post-metadata">

**Author:** ![eduzal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eduzal/32/759_2.png) [@eduzal](https://discourse.processing.org/u/eduzal)\
**Post date:** [September 20, 2018, 4:28pm UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697/5 "2018-09-20T16:28:43Z")

</div>

hi chisir  
`i>=0` doesn’t clear the array, just deactivates it. when turned on again, it continues from where it stopped.

using `r.rings.clear()` it returns

```auto
Sep 20, 2018 1:24:32 PM controlP5.ControlBroadcaster printMethodError
SEVERE: An error occured while forwarding a Controller event, please check your code at ringToggle
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at controlP5.ControlBroadcaster.invokeMethod(Unknown Source)
	at controlP5.ControlBroadcaster.callTarget(Unknown Source)
	at controlP5.ControlBroadcaster.broadcast(Unknown Source)
	at controlP5.Controller.broadcast(Unknown Source)
	at controlP5.Toggle.setState(Unknown Source)
	at controlP5.Toggle.setValue(Unknown Source)
	at controlP5.Toggle.setValue(Unknown Source)
	at dome_004$ControlFrame.setup(dome_004.java:356)
	at processing.core.PApplet.handleDraw(PApplet.java:2404)
	at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
Caused by: java.lang.NullPointerException
	at dome_004$ControlFrame.ringToggle(dome_004.java:467)
	... 15 more

```

using, erasing one by one

```auto
for (int i=0; i<r.rings.size(); i++) {
        Ring ri=r.rings.get(i);
        r.rings.remove(i);
      }

```

returns

```auto
Sep 20, 2018 1:27:21 PM controlP5.ControlBroadcaster printMethodError
SEVERE: An error occured while forwarding a Controller event, please check your code at ringToggle
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at controlP5.ControlBroadcaster.invokeMethod(Unknown Source)
	at controlP5.ControlBroadcaster.callTarget(Unknown Source)
	at controlP5.ControlBroadcaster.broadcast(Unknown Source)
	at controlP5.Controller.broadcast(Unknown Source)
	at controlP5.Toggle.setState(Unknown Source)
	at controlP5.Toggle.setValue(Unknown Source)
	at controlP5.Toggle.setValue(Unknown Source)
	at dome_004$ControlFrame.setup(dome_004.java:356)
	at processing.core.PApplet.handleDraw(PApplet.java:2404)
	at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
Caused by: java.lang.NullPointerException
	at dome_004$ControlFrame.ringToggle(dome_004.java:468)
	... 15 more

```

---

<div class="post-metadata">

**Author:** ![eduzal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eduzal/32/759_2.png) [@eduzal](https://discourse.processing.org/u/eduzal)\
**Post date:** [September 20, 2018, 4:34pm UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697/6 "2018-09-20T16:34:02Z")

</div>

by creating a boolen cleared you mean

```auto
  boolean cleared() {
    if (rings.size()==0) {
      return true;
    } else {
      return false;
    }
  }
  void display() {
    if (cleared()) {
      for (int i=rings.size()-1; i>0; i--) {
        Ring r=rings.get(i);
        r.update();
        r.display();
        if (r.isDead()) {
          rings.remove(i);
        }
      }
    }
  }

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 20, 2018, 4:49pm UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697/7 "2018-09-20T16:49:34Z")

</div>

Does it work?

I meant something different.

More like

`boolean cleared = false;` as a global variable

```auto
r.rings.clear();
cleared = true; 

```

and in display()

```auto
void display(){
   for(int i=rings.size()-1; i>0; i--){
      if(cleared) return; 

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [September 20, 2018, 4:50pm UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697/8 "2018-09-20T16:50:06Z")

</div>

Interesting can’t see the IndexOutOfBoundsException exception 🙂  
What is also interesting is you get the same exception using `clear()` or the loop, so use `clear()`

What is interesting is the `java.lang.reflect.InvocationTargetException`. This is caused when it can’t find the method to execute.

Looking at the rest of the stack trace I see

```auto
	at dome_004$ControlFrame.setup(dome_004.java:356)
	at processing.core.PApplet.handleDraw(PApplet.java:2404)

```

It seems that the PApplet’s draw() method is being called before the controlFrame setup() which probably means that the toggle switch is not been configured so the ringToggle method is not been ‘set’. Looking at the rest of the trace seems to confirm this

```auto
Caused by: java.lang.NullPointerException
	at dome_004$ControlFrame.ringToggle(dome_004.java:468)

```

Sorry for qll the seems and maybes I am not familiar with the inner workings of controlP5, being more used to G4P.

There is another possibility and that is because your cp5 toggle is on a cp5 control frame, effectively a second window. Again depending on controlP5 implements the ControlFrame. If it does it in a similar manner to G4P then each window has its own event handling thread so they can get out of sync.

You might try and read this [discussion](https://discourse.processing.org/t/g4p-gtextarea-glabel-object-update-across-windows-cause-app-crash/2854).

---

<div class="post-metadata">

**Author:** ![eduzal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eduzal/32/759_2.png) [@eduzal](https://discourse.processing.org/u/eduzal)\
**Post date:** [September 20, 2018, 5:28pm UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697/9 "2018-09-20T17:28:42Z")

</div>

i’ve opened up a request on controlp5’s github. who knows when they will answer.  
also the ControlP5frames example is not working. i’m using processing 3.4

---

<div class="post-metadata">

**Author:** ![eduzal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eduzal/32/759_2.png) [@eduzal](https://discourse.processing.org/u/eduzal)\
**Post date:** [September 20, 2018, 5:52pm UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697/10 "2018-09-20T17:52:26Z")

</div>

for right now i’ll just change the toggle to control particle spawning, instead of clearing the arraylist, since adding a ControlListener didn’t work at all

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 20, 2018, 7:46pm UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697/11 "2018-09-20T19:46:45Z")

</div>

> <https://github.com/sojamo/controlp5/issues/138>

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [September 20, 2018, 11:37pm UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697/12 "2018-09-20T23:37:30Z")

</div>

> [@quark](#):
>
> What is interesting is the `java.lang.reflect.InvocationTargetException` . This is caused when it can’t find the method to execute.

That’s not the cause of that exception. It means the method being invoked is throwing an exception. Neither is draw being called before setup.

My guess is that it’s because the line constructing the ControlFrame needs moving to the end of setup so that the ring system is constructed first. This may not be a complete fix if threading is involved though.

---

<div class="post-metadata">

**Author:** ![eduzal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eduzal/32/759_2.png) [@eduzal](https://discourse.processing.org/u/eduzal)\
**Post date:** [September 21, 2018, 1:25am UTC](https://discourse.processing.org/t/clearing-an-arraylist/3697/13 "2018-09-21T01:25:25Z")

</div>

This fix, instatiating the ControlFrame for last in the setup, really worked.  
didn’t survived a lot of vandalism but cleared the function anyway.
