# Why does my program return the error "ArrayIndex Out Of Bounds Exception: 4"?

**URL:** <https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651>\
**Category:** Coding Questions\
**Created:** [November 21, 2021, 12:03pm UTC](https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651 "2021-11-21T12:03:26Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [November 21, 2021, 12:03pm UTC](https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651/1 "2021-11-21T12:03:26Z")

</div>

the number 4 is not in my code so I don’t know why it gives this error.  
this is my code:

```auto
String blocked = "iA";
String inlava = "iB";
String nearsick = "iC";

String moverandom = "oA";
String movewest = "oB";
String moveeast = "oC";
String movenorth = "oD";
String movesouth = "oE";

String[] input = {blocked, inlava, nearsick};

String[] genome = new String[51];

float[] x = new float[51];
float[] y = new float[51];

boolean beingblocked = false;
String[] genpart = new String[204];

void setup() {
  fullScreen();
  for (int i= 0; i<=50; i++) {
    genome[i] = "i"+char(int(random(65, 67))) +"1" + "o"+char(int(random(65, 69))) +"1" + "i"+char(int(random(65, 67))) + "1" + "o"+char(int(random(65, 69)));
  }
  for (int i=0; i<=204; i++) {
    int gi=0;
    genpart= split(genome[gi], '1');
    gi++;
    print(genpart[i] + " ");
  }
}

void draw() {
  if (genpart[0]==blocked || genpart[2]==blocked && genpart[1]== moverandom || genpart[3]==moverandom) {
    if (beingblocked) {
      x[0]= x[0]+(int(random(-1, 1)))*10;
    }
  }
}

```

---

<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:** [November 21, 2021, 12:05pm UTC](https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651/2 "2021-11-21T12:05:35Z")

</div>

> [@MoonAlien822](#):
>
> `int i=0; i<=204; i++) {`

Instead of \<= try \<

Regards

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [November 21, 2021, 12:13pm UTC](https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651/4 "2021-11-21T12:13:13Z")

</div>

like this?

```auto
String blocked = "iA";
String inlava = "iB";
String nearsick = "iC";

String moverandom = "oA";
String movewest = "oB";
String moveeast = "oC";
String movenorth = "oD";
String movesouth = "oE";

String[] input = {blocked, inlava, nearsick};

String[] genome = new String[51];

float[] x = new float[51];
float[] y = new float[51];

boolean beingblocked = false;
String[] genpart = new String[204];

void setup() {
  fullScreen();
  for (int i= 0; i<=50; i++) {
    genome[i] = "i"+char(int(random(65, 67))) +"1" + "o"+char(int(random(65, 69))) +"1" + "i"+char(int(random(65, 67))) + "1" + "o"+char(int(random(65, 69)));
  }
  for (int i=0; i<204; i++) {
    int gi=0;
    genpart= split(genome[gi], '1');
    gi++;
    print(genpart[i] + " ");
  }
}

void draw() {
  if (genpart[0]==blocked || genpart[2]==blocked && genpart[1]== moverandom || genpart[3]==moverandom) {
    if (beingblocked) {
      x[0]= x[0]+(int(random(-1, 1)))*10;
    }
  }
}

```

because this did not work

---

<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:** [November 21, 2021, 12:28pm UTC](https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651/5 "2021-11-21T12:28:48Z")

</div>

You could use the array length in your loop so even if you change the array size the code will not cause an error.

`for(int i = 0; i < genpart.length; 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:** [November 21, 2021, 2:44pm UTC](https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651/6 "2021-11-21T14:44:25Z")

</div>

usually, we just use \< in a for loop and not \<=

---

<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:** [November 21, 2021, 2:50pm UTC](https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651/7 "2021-11-21T14:50:25Z")

</div>

> [@MoonAlien822](#):
>
> ```auto
> for (int i=0; i<204; i++) {
> int gi=0;
> genpart= split(genome[gi], '1');
> gi++;
> 
> ```

here is also an error. See gi : you say gi++ and it runs up to 203 but you use it as index for genome which only goes to 51.

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [November 21, 2021, 5:15pm UTC](https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651/9 "2021-11-21T17:15:36Z")

</div>

i dont even remember why i had gi, anyways this gives the same error

```auto
String blocked = "iA";
String inlava = "iB";
String nearsick = "iC";

String moverandom = "oA";
String movewest = "oB";
String moveeast = "oC";
String movenorth = "oD";
String movesouth = "oE";

String[] input = {blocked, inlava, nearsick};

String[] genome = new String[51];

float[] x = new float[51];
float[] y = new float[51];

boolean beingblocked = false;
String[] genpart = new String[204];

void setup() {
  fullScreen();
  for (int i= 0; i<=50; i++) {
    genome[i] = "i"+char(int(random(65, 67))) +"1" + "o"+char(int(random(65, 69))) +"1" + "i"+char(int(random(65, 67))) + "1" + "o"+char(int(random(65, 69)));
  }
  for (int i=0; i<204; i++) {
    genpart= split(genome[i], '1');
    print(genpart[i] + " ");
  }
}

void draw() {
  if (genpart[0]==blocked || genpart[2]==blocked && genpart[1]== moverandom || genpart[3]==moverandom) {
    if (beingblocked) {
      x[0]= x[0]+(int(random(-1, 1)))*10;
    }
  }
}

```

---

<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:** [November 21, 2021, 8:17pm UTC](https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651/10 "2021-11-21T20:17:48Z")

</div>

```auto
    genpart= split(genome[i], '1');

```

here you redefine genpart

Hence it has not `204` slots as you might expect but as many as split() returns, presumably 3.

**Solution**

so replace ` print(genpart[i] + " ");` with ` println(genpart + " ");` or even use printArray but without the [i]

OR say

```auto
// using i2 here within the for loop with i
for (int i2=0; i2<genpart.length; i2++) {
  print(genpart[i2] + " "); 
}

```

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [November 21, 2021, 8:42pm UTC](https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651/11 "2021-11-21T20:42:51Z")

</div>

my code generates a gene, this gene consists of two different things, inputs “i” and outputs “o”. they have 2 inputs and 2 outputs it and the parts are seperated by a "1 then cuts this gene into multiple pieces, so the first input will be genpart[0] and the first output will be genpart[1], what I am trying to achieve in the moment is having it do this for every gene, since it generates 51 of them. but i cannot figure out how to do this, as appareantly it changes the array length when you do this once. how do I make it work?

---

<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:** [November 21, 2021, 9:03pm UTC](https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651/12 "2021-11-21T21:03:37Z")

</div>

> [@MoonAlien822](#):
>
> how do I make it work?

I just explained it:

a 2nd for loop within the for loop

```auto
// using i2 here within the for loop with i
for (int i2=0; i2<genpart.length; i2++) {
  print(genpart[i2] + " "); 
}

```

---

<div class="post-metadata">

**Author:** ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)\
**Post date:** [November 22, 2021, 6:54am UTC](https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651/13 "2021-11-22T06:54:04Z")

</div>

Why would this fix it?

---

<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:** [November 22, 2021, 11:38am UTC](https://discourse.processing.org/t/why-does-my-program-return-the-error-arrayindex-out-of-bounds-exception-4/33651/14 "2021-11-22T11:38:27Z")

</div>

Because it takes into account the current length of the array genPart

Try it

**Example**

```auto
String blocked = "iA";
String inlava = "iB";
String nearsick = "iC";

String moverandom = "oA";
String movewest = "oB";
String moveeast = "oC";
String movenorth = "oD";
String movesouth = "oE";

String[] input = {blocked, inlava, nearsick};

String[] genome = new String[51];

float[] x = new float[51];
float[] y = new float[51];

boolean beingblocked = false;
String[] genpart; // = new String[204];

void setup() {

  noLoop();

  // fullScreen();
  size(333, 333);

  for (int i= 0; i<=50; i++) {
    genome[i] = "i"+char(int(random(65, 67))) +"1" + "o"+char(int(random(65, 69))) +"1" + "i"+char(int(random(65, 67))) + "1" + "o"+char(int(random(65, 69)));
  }
  for (int i=0; i<51; i++) {
    genpart = split(genome[i], '1');
    // print(genpart[i] + " ");
    // using i2 here within the for loop with i
    for (int i2=0; i2<genpart.length; i2++) {
      print(genpart[i2] + " ");
    }
    println();
  }
}

void draw() {
  if (genpart[0]==blocked || 
    genpart[2]==blocked && 
    genpart[1]== moverandom || 
    genpart[3]==moverandom) {
    if (beingblocked) {
      x[0]= x[0]+(int(random(-1, 1)))*10;
    }
  }
}
//

```
