# Array index out of bounds question

**URL:** <https://discourse.processing.org/t/array-index-out-of-bounds-question/25870>\
**Category:** Coding Questions\
**Created:** [December 1, 2020, 6:23pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-question/25870 "2020-12-01T18:23:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![karenben](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@karenben](https://discourse.processing.org/u/karenben)\
**Post date:** [December 1, 2020, 6:23pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-question/25870/1 "2020-12-01T18:23:55Z")

</div>

What’s causing the ArrayIndexOutOfBoundsException: 5?  
I want to make each bounce have a function that increases the value of the counter per ball. If the counter is greater than 10, stop drawing the ball

int bounces = 5;  
float[] x = new float [bounces];  
float[] y = new float [bounces];  
float[] xspeed = new float[bounces];  
float[] yspeed = new float[bounces];  
int[] types = new int[bounces];  
boolean alive = true;  
color c;

color bgc;

void setup()  
{

size(700, 700);  
noStroke();  
for (int i = 0; i \< bounces; ++i)  
{  
x[i] = random(width);  
y[i] = random(height);  
xspeed[i] = random(-5, 5);  
yspeed[i] = random(-5, 5);  
c = color(random(100, 255), random(100, 255), random(100, 255));  
bgc = color(random(100, 255), random(100, 255), random(100, 255));  
textSize(25);  
textAlign(CENTER, CENTER);  
}  
}

void draw()  
{  
//fondo negro  
background(bgc);

for (int i = 0; i \< bounces; ++i)  
{  
moveEllipse(i);  
bounceEllipse(i);  
drawEllipse (i);  
}  
}

void moveEllipse(int id)  
{  
x[id] += xspeed[id];  
y[id] += yspeed[id];  
}

void bounceEllipse(int id)  
{  
if (x[id] \< 0 || x[id] \> width && bounces \>9)  
{  
xspeed[id] \*= -1;  
bounces = bounces +1;  
}  
if (y[id] \< 0 || y[id] \> height && bounces \>9)  
{  
yspeed[id] \*= -1;  
bounces = bounces +1;  
{  
fill(0);  
text(bounces, x[id], y[id]);  
}  
}  
}

void drawEllipse(int id)  
{  
float xoff = (float)width / bounces;  
fill(255);  
ellipse(x[id], y[id], 50, 50);  
}

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 1, 2020, 7:07pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-question/25870/2 "2020-12-01T19:07:17Z")

</div>

Hi, @karenben please edit your code above by selecting it and pressing the \</\> button.  
Without testing, I can see that you are increasing the value of the bounces, and at the same time calling the x[] and y[] array with this value higher than 5. This will give the ArrayIndexOutOfBoundsException error.

---

<div class="post-metadata">

**Author:** ![karenben](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@karenben](https://discourse.processing.org/u/karenben)\
**Post date:** [December 1, 2020, 10:29pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-question/25870/3 "2020-12-01T22:29:38Z")

</div>

Hi @noel thanks for answering me! I have a problem. I don’t know what I did wrong but this is the exercise from the beginning. Now the number on the ball doesn’t appear anymore can you help me? I’m stuck  
/ \*

1. Pass the exercise to logic of arrays and cycles for
2. Each bounce will have a function that increases the value of the counter per ball.
3. If the counter is greater than 10, stop drawing the ball
4. When all the balls disappear print a message that says: “I love the code”

- /

// initial variables for the ball  
int bounces = 0;  
float x = 0;  
float y = 0;  
float xspeed = 0;  
float yspeed = 0;  
boolean alive = true;  
C color;

// variable for the background color  
bgc color;

void setup ()  
{  
// we assign the dimensions of the canvas  
size (700, 700);  
// we assign that the ball will not have a stroke  
noStroke ();  
// we assign initial values ​​to the variables of the ball  
x = random (width);  
y = random (height);  
xspeed = random (-5, 5);  
yspeed = random (-5, 5);  
c = color (random (100, 255), random (100, 255), random (100, 255));  
// we assign initial values ​​for the background color  
bgc = color (random (100, 255), random (100, 255), random (100, 255));  
// we assign the font size of the letter  
textSize (50);  
// we justify the text vertically and horizontally centered  
textAlign (CENTER, CENTER);  
}

void draw ()  
{  
//black background  
background (bgc);

// we increase the position of the object by its speed to achieve the illusion of movement  
x + = xspeed;  
y + = yspeed;

// logic for bouncing the ball  
if (x \<0 || x\> width)  
{  
xspeed \* = -1;  
}  
if (y \<0 || y\> height)  
{  
yspeed \* = -1;  
}

// we draw the ball  
fill (255);  
ellipse (x, y, 50, 50);  
fill (0);  
text (bounces, x, y);  
}

//This is how I solved it:  
/ \*

1. Pass the exercise to logic of arrays and cycles for
2. Each bounce will have a function that increases the value of the counter per ball.
3. If the counter is greater than 10, stop drawing the ball
4. When all the balls disappear print a message that says: “I love the code”

- /

// initial variables for the ball  
int bounces = 10;  
int ellipse = 5;  
float [] x = new float [bounces];  
float [] y = new float [bounces];  
float [] xspeed = new float [bounces];  
float [] yspeed = new float [bounces];  
int [] types = new int [bounces];  
boolean alive = true;  
C color;

// variable for the background color  
bgc color;

void setup ()  
{  
// we assign the dimensions of the canvas  
size (700, 700);  
// we assign that the ball will not have a stroke  
noStroke ();  
// we assign initial values ​​to the variables of the ball  
for (int i = 0; i \<ellipse; ++ i)  
{  
x [i] = random (width);  
y [i] = random (height);  
xspeed [i] = random (-5, 5);  
yspeed [i] = random (-5, 5);  
c = color (random (100, 255), random (100, 255), random (100, 255));  
// we assign initial values ​​for the background color  
bgc = color (random (100, 255), random (100, 255), random (100, 255));  
// we assign the font size of the letter  
textSize (25);  
// we justify the text vertically and horizontally centered  
textAlign (CENTER, CENTER);  
}  
}

void draw ()  
{  
//black background  
background (bgc);

for (int i = 0; i \<ellipse; ++ i)  
{  
moveEllipse (i);  
bounceEllipse (i);  
drawEllipse (i);  
}  
}

// we increase the position of the object by its speed to achieve the illusion of movement  
void moveEllipse (int id)  
{  
x [id] + = xspeed [id];  
y [id] + = yspeed [id];  
}

// logic for bouncing the ball  
void bounceEllipse (int id)  
{  
if (x [id] \<0 || x [id]\> width && bounces\> 9)  
{  
xspeed [id] \* = -1;  
bounces = bounces +1;  
}  
if (y [id] \<0 || y [id]\> height && bounces\> 9)  
{  
yspeed [id] \* = -1;  
bounces = bounces +1;  
{  
fill (0);  
text (bounces, x [id], y [id]);  
}  
}  
}

// we draw the ball  
void drawEllipse (int id)  
{  
float xoff = (float) width / bounces;  
fill (255);  
ellipse (x [id], y [id], 50, 50);  
}

---

<div class="post-metadata">

**Author:** ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)\
**Post date:** [December 1, 2020, 10:34pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-question/25870/4 "2020-12-01T22:34:29Z")

</div>

Please try to edit your posts and format it with the code button. Once in a code box it can be easily copied with a button that appears above the vertical scrollbar.

---

<div class="post-metadata">

**Author:** ![karenben](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@karenben](https://discourse.processing.org/u/karenben)\
**Post date:** [December 1, 2020, 10:46pm UTC](https://discourse.processing.org/t/array-index-out-of-bounds-question/25870/5 "2020-12-01T22:46:51Z")

</div>

@noel which is the code button?
