# ArrayoutOfBoundsException

**URL:** <https://discourse.processing.org/t/arrayoutofboundsexception/4182>\
**Category:** Coding Questions\
**Created:** [October 6, 2018, 7:09am UTC](https://discourse.processing.org/t/arrayoutofboundsexception/4182 "2018-10-06T07:09:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FreshlyChicken](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@FreshlyChicken](https://discourse.processing.org/u/FreshlyChicken)\
**Post date:** [October 6, 2018, 7:09am UTC](https://discourse.processing.org/t/arrayoutofboundsexception/4182/1 "2018-10-06T07:09:32Z")

</div>

Hello,

First I show you my code.

```auto
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

---

<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:** [October 6, 2018, 7:27am UTC](https://discourse.processing.org/t/arrayoutofboundsexception/4182/2 "2018-10-06T07:27:08Z")

</div>

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!

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

```

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

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [October 6, 2018, 7:27am UTC](https://discourse.processing.org/t/arrayoutofboundsexception/4182/3 "2018-10-06T07:27:38Z")

</div>

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](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147)

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:

```auto
if (randomZahl[i] == a)

```

You are using it in the following for loop:

```auto
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:

```auto
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:

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

```

or even more fancy:

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

```

---

<div class="post-metadata">

**Author:** ![FreshlyChicken](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@FreshlyChicken](https://discourse.processing.org/u/FreshlyChicken)\
**Post date:** [October 6, 2018, 7:29am UTC](https://discourse.processing.org/t/arrayoutofboundsexception/4182/4 "2018-10-06T07:29:30Z")

</div>

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