We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › "ArrayIndexOutOfBoundsException: 10" error message
Page Index Toggle Pages: 1
"ArrayIndexOutOfBoundsException: 10" error message (Read 926 times)
"ArrayIndexOutOfBoundsException: 10" error message
Dec 16th, 2009, 7:12am
 
I can't figure out why this code won't work, for an array which loops through itself and draws rotating circles. I get an "ArrayIndexOutOfBoundsException: 10" error message.    

Thanks.                                                                                                                              

int count = 10;
float r;
float theta;
float theta_vel;
float theta_acc;
float[] x  = new float[count];
float[] y  = new float[count];

void setup()
{
 size(500,500);
 
 int z = 0;
 
 /*while(z < x.length)
 {
   for (x[z]=100;x[z]<width;x[z]=x[z]+100)
   {
     for (y[z]=100;y[z]<height;y[z]=y[z]+400)
     {
   x[z] = (100);
   y[z] = (100);
   

     }
   }*/
   
 }


float distance;

void draw()
{
 
 int z = 0;
 
 while(z < x.length)
 {
   for (x[z]=100;x[z]<500;x[z]=x[z]+100)
   {
     for (y[z]=100;y[z]<500;y[z]=y[z]+400)
     {
   x[z] = (100);
   y[z] = (100);
   

     }
     z=z+1;
   }
   
 while(z < x.length)
 {
   
   
   distance = dist(mouseX,mouseY,x[z],y[z]);
   if(distance<10)
   {
     r = 10.0f;
     theta = 0.0f;
     
    x[z] = r * cos(theta);
    y[z] = r * sin(theta);
    theta_vel += theta_acc;
    theta += theta_vel;
   
    ellipse(x[z] , y[z], 8, 8);
   
 }
}
    z++;
}
}
Re: "ArrayIndexOutOfBoundsException: 10" error message
Reply #1 - Dec 16th, 2009, 7:22am
 
int z = 0;

while(z < x.length) {
 for (x[z]=100;x[z]<500;x[z]=x[z]+100) {
   for (y[z]=100;y[z]<500;y[z]=y[z]+400) {
     x[z] = (100);
     y[z] = (100);
   }
   z=z+1;
 }
...

you are incrementing z inside your outer for loop there. it will go above 10 in that case.

using an array element (x[z]) as the controlling variable for your loops is also a bit unusual... i really have no idea what that code is doing.
Re: "ArrayIndexOutOfBoundsException: 10" error message
Reply #2 - Dec 16th, 2009, 7:22am
 
also this:

I get an "ArrayIndexOutOfBoundsException: 10" error message.    

is more useful if you tell us the line it's happening on.
Re: "ArrayIndexOutOfBoundsException: 10" error message
Reply #3 - Dec 16th, 2009, 7:41am
 
One tip to start you off resolving this.  When iterating through a single array (i.e. doing something to each item in the array) it's more usual to use a single for loop:

Code:
int count;
float[] x;

void setup() {
size(300,300);
 count = 10;
 x = new float[count];

 for(int i=0; i<x.length; i++) {
   x[i] = i;
 }

}


You can use a while loop but because the control of a for loop is self-contained you're less likely to land up in an infinite loop (e.g. by forgetting to increment the counter)...

Like koogy I've no idea what your nested for loops with array elements are supposed to do  Shocked

If you're trying to implement some code you found online it might help if you post a link.
Page Index Toggle Pages: 1