ArrayoutOfBoundsException


#1

Hello,

First I show you my code.

int[] randomZahl = {3,6,8,9,4};

void setup(){  
  
  size(500,500);
  
}

void draw(){
  
  line(100,0,100,500);
  line(200,0,200,500);
  line(300,0,300,500);
  line(400,0,400,500);
  line(500,100,0,100);
  line(500,200,0,200);
  line(500,300,0,300);
  line(500,400,0,400);
  
}

void mouseClicked(){
  
  int a = 0;
  
  //Zeile 1
 
if(mouseX<=100 && mouseY<=100){
   rect(0,0,100,100);
   a = 1;
  }
  
else if(mouseX<=200 && mouseX>=100 && mouseY<=100 && mouseY>=0){
   rect(100,0,100,100);
   a = 2;
  }
  
else if(mouseX<=300 && mouseX>=200 && mouseY<=100 && mouseY>=0){
   rect(200,0,100,100);
   a = 3;
  }
  
else if(mouseX<=400 && mouseX>=300 && mouseY<=100 && mouseY>=0){
   rect(300,0,100,100);
   a = 4;
  }
   
else if(mouseX<=500 && mouseX>=400 && mouseY<=100 && mouseY>=0){
   rect(400,0,100,100);
   a = 5;
  }   

 //Zeile 2
 
else if(mouseX<=100 && mouseX>=0 && mouseY<=200 && mouseY>=100){
   rect(0,100,100,100);
   a = 6;
  }
  
else if(mouseX<=200 && mouseX>=100 && mouseY<=200 && mouseY>=100){
   rect(100,100,100,100);
   a = 7;
  }
  
else if(mouseX<=300 && mouseX>=200 && mouseY<=200 && mouseY>=100){
   rect(200,100,100,100);
   a = 8;
  }
  
else if(mouseX<=400 && mouseX>=300 && mouseY<=200 && mouseY>=100){
   rect(300,100,100,100);
   a = 9;
  }
  
else if(mouseX<=500 && mouseX>=400 && mouseY<=200 && mouseY>=100){
   rect(400,100,100,100);
   a = 10;
  }
  
  //Zeile 3
  
else if(mouseX<=100 && mouseX>=0 && mouseY<=300 && mouseY>=200){
   rect(0,200,100,100);
   a = 11;
  }
  
else if(mouseX<=200 && mouseX>=100 && mouseY<=300 && mouseY>=200){
   rect(100,200,100,100);
   a = 12;
  }
  
else if(mouseX<=300 && mouseX>=200 && mouseY<=300 && mouseY>=200){
   rect(200,200,100,100);
   a = 13;
  }
  
else if(mouseX<=400 && mouseX>=300 && mouseY<=300 && mouseY>=200){
    fill(0);
   rect(300,200,100,100);
   a = 14;
  }
  
else if(mouseX<=500 && mouseX>=400 && mouseY<=300 && mouseY>=200){
   rect(400,200,100,100);
   a =  15;
  } 

  //Zeile 4

else if(mouseX<=100 && mouseX>=0 && mouseY<=400 && mouseY>=300){
   rect(0,300,100,100);
   a = 16;
  } 
  
else if(mouseX<=200 && mouseX>=100 && mouseY<=400 && mouseY>=300){
   rect(100,300,100,100);
   a = 17;
  } 
  
else if(mouseX<=300 && mouseX>=200 && mouseY<=400 && mouseY>=300){
   rect(200,300,100,100);
   a = 18;
  } 
  
else if(mouseX<=400 && mouseX>=300 && mouseY<=400 && mouseY>=300){
   rect(300,300,100,100);
   a = 19;
  }   
  
else if(mouseX<=500 && mouseX>=400 && mouseY<=400 && mouseY>=300){
   rect(400,300,100,100);
   a = 20;
  }   
  
  //Zeile 5
  
else if(mouseX<=100 && mouseX>=0 && mouseY<=500 && mouseY>=400){
   rect(0,400,100,100);
   a = 21;
  }  
  
else if(mouseX<=200 && mouseX>=100 && mouseY<=500 && mouseY>=400){
   rect(100,400,100,100);
   a = 22;
  } 
  
else if(mouseX<=300 && mouseX>=200 && mouseY<=500 && mouseY>=400){
   rect(200,400,100,100);
   a = 23;
  }  
  
else if(mouseX<=400 && mouseX>=300 && mouseY<=500 && mouseY>=400){
   rect(300,400,100,100);
   a = 24;
  }
  
else if(mouseX<=500 && mouseX>=400 && mouseY<=500 && mouseY>=400){
   rect(400,400,100,100);
   a = 25;
  } 

for(int i = 0; i < randomZahl.length; ++i){

if(randomZahl[i] == a){
 fill(0);
  }
}   
  
}

When I run the programm I get the message “ArrayIndexOutOfBoundsException” I know what it mean but I dont know how to solve. Can anyone help me? Maybe with an example how to solve?

Thank You


#2

Your array is only 5 numbers long, so you can only access elements at index 0, 1, 2, 3, and 4. Your for loop goes up to 25!

for(int i = 0; i <= 25;++i){

You can’t even access randomZahl[5], let alone randomZahl[24]!


#3

Hi FreshlyChicken,

I can see that you tried to format your post but you didn’t use it correctly. Read this thread for more info: Guidelines—Tips on Asking Questions

Now to get back to your question, I’m not sure you really know what it means because the fix is quite obvious when you figured out where it is coming from. You are getting this error because you try to access an element of an array that does not exist because the array is too short.

In this case the bug is on this line:

if (randomZahl[i] == a)

You are using it in the following for loop:

for (int i = 0; i <= 25; ++i) {
  if (randomZahl[i] == a) {
    fill(0);
  }
}

You are making i going from 0 to 25 so at some point i will be 25 and thus you will be trying to access the 25th element of the randomZah array. But if we look at how you defined your array, we can see that it has only 5 elements:

int[] randomZahl = {3, 6, 8, 9, 4};

so you can only access element from index 0 to 5.

To cancel your errror you then only need to change your for loop to not exceed the size of your array:

for (int i = 0; i < 5; ++i) {
  if (randomZahl[i] == a) {
    fill(0);
  }
}

or even more fancy:

for (int i = 0; i < randomZahl.length; ++i) {
  if (randomZahl[i] == a) {
    fill(0);
  }
}

#4

Thank You. 1 minute ago I found the solution for my own. But still thank you