# Error when splice() is called

**URL:** https://discourse.processing.org/t/error-when-splice-is-called/30079
**Category:** Beginners
**Created:** [May 13, 2021, 8:22am UTC](https://discourse.processing.org/t/error-when-splice-is-called/30079 "2021-05-13T08:22:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Boutrup98](https://avatars.discourse-cdn.com/v4/letter/b/bbe5ce/32.png) [@Boutrup98](https://discourse.processing.org/u/Boutrup98)
#### Post date: [May 13, 2021, 8:22am UTC](https://discourse.processing.org/t/error-when-splice-is-called/30079/1 "2021-05-13T08:22:40Z")

</div>

Hello everybody!

We have a problem with the splice() syntax.

Our program works by generating a lot of bubbles, and the bubbles then start to consume each other and will then grow bigger.

The problem occurs randomly and poses the following error.

You can find the code [here](https://gitlab.com/Boutrup98/p5js/-/tree/master/test)

Thank you in advance 🙌

 ![Screenshot 2021-05-13 at 10.14.32](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/7180a6971df83a3583ac997c0411efce02e7a619.png)

---

<div class="post-metadata">

### Author: ![Boutrup98](https://avatars.discourse-cdn.com/v4/letter/b/bbe5ce/32.png) [@Boutrup98](https://discourse.processing.org/u/Boutrup98)
#### Post date: [May 13, 2021, 8:35am UTC](https://discourse.processing.org/t/error-when-splice-is-called/30079/2 "2021-05-13T08:35:57Z")

</div>

@shiffman plz help 🙏

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [May 13, 2021, 9:09am UTC](https://discourse.processing.org/t/error-when-splice-is-called/30079/3 "2021-05-13T09:09:44Z")

</div>

Hi! Welcome to the forum!

I know you need help but please be patient 😅 In fact Dan explains in some of his videos about the splice trick.

[![](https://img.youtube.com/vi/biN3v3ef-Y0/hqdefault.jpg "Coding Challenge #5: Space Invaders in JavaScript with p5.js") ](https://www.youtube.com/watch?v=biN3v3ef-Y0&t=1792)

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [May 13, 2021, 9:21am UTC](https://discourse.processing.org/t/error-when-splice-is-called/30079/4 "2021-05-13T09:21:42Z")

</div>

You’re not checking very clearly to make sure that you’re always accessing a Bubble in the `bubbles` that still exists.

I suspect that you are trying to remove the smallest of the two bubbles when two bubbles collide. But what if , for example, you have three bubbles in the array, and the smallest one is the second one, and it collides with the third one? You would remove the second one from the array, and then try to increase the size of the third one… BUT THERE ISN’T A THIRD ONE ANY MORE! You JUST shrunk the array when you called splice! And that’s why you get an error.

Instead, I would simplify the logic. First, change it so that when you draw a bubble, if that bubble has a size of 0, just don’t draw it. Then, when checking for colliding bubbles, increase the size of one, and set the other to have a size of 0. Also add a check to make sure you are not trying to check for collisions of bubbles that have a size of zero. Finally, after all the collisions are done, loop over the array of bubbles BACKWARDS, and remove any (using splice) that have a size of zero.

```auto
// ---
void draw(){
  if( this.r == 0 ){ return; }
  // ...
}
// ---
for( i ){
  for( j ){
    if( i != j && bubbles[i].r != 0 && bubbles[j] != 0 ){
      if( collides(i, j ) ){
        if ( bubbles[i].r < bubbles[j].r ){
          bubbles[i].r = 0;
          bubbles[j].r += 10;
        } else {
          // The other way.
}}}}}}}}} // Or whatever.
for( i = bubbles.length - 1; i >= 0; i-- ){ // Loop backwards to not goof up indexes while removing things.
  if( bubbles[i].r == 0 ){
    bubbles.splice( i , 1 );
  }
}

```

Also, make sure that you’re using proper constants in your bubble classes’s `intersect()` function. Consider this line:

```auto
		if (d < bubbles[0].r + bubbles[1].r) {

```

This will break if you only have one bubble!
