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

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

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;
    }
  }
}

Instead of <= try <

Regards

2 Likes

like this?

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

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++) {

1 Like

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

1 Like

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.

1 Like

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

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;
    }
  }
}
    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

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

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?

I just explained it:

a 2nd for loop within the for loop

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

Why would this fix it?

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

Try it

Example

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;
    }
  }
}
//

1 Like