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 & HelpPrograms › Strange ArrayIndexOutOfBoundsException Errors
Page Index Toggle Pages: 1
Strange ArrayIndexOutOfBoundsException Errors (Read 561 times)
Strange ArrayIndexOutOfBoundsException Errors
Apr 24th, 2009, 2:07pm
 
I'm working on a program to make abstract art (yes, I did just say that), but it keeps crashing with that error. Can somebody help? Here's the code:
Code:
int numTrees=1;
Tree[] tree;
//int iteration[][]

void setup() {
size(500,500);
tree=new Tree[numTrees+1];
tree[0]=new Tree(0,0,0);
print("iteration 1");
// iteration[1][1]=0;
// iteration[1][2]=0;
// iteration[1][3]=0;
}

void draw() {
print("iterating");
for(int i=0; i < (numTrees); i++) {
tree[i].iterate();
}
}

class Tree {
int points[][];
int itCount;
Tree(int x,int y,int itCountT) {
itCount=itCountT;
points=new int[itCount+1][2];
points[itCount][0]=x;
points[itCount][1]=y;
}
void iterate() {
itCount=itCount+1;
expand(points,itCount+1);
for(int i=0;i<3;i++) {
points[itCount][i]=points[itCount-1][i]+int(random(0,3));
}
if (int(random(3))==1) {
numTrees=numTrees+1;
expand(tree,numTrees+1);
if (itCount>0) {
tree[numTrees]=new Tree(points[itCount-1][0],points[itCount][1],itCount);
}
}
for (int i=0;i<=itCount;i++) {
ellipse(points[itCount][0],points[itCount][1],6,6);
print(points[itCount][0]);
print(points[itCount][1]);
}
}
}

Re: Strange ArrayIndexOutOfBoundsException Errors
Reply #1 - Apr 24th, 2009, 2:55pm
 
Could you describe what each part (for loops specifically) of the iterate method is supposed to do?
Re: Strange ArrayIndexOutOfBoundsException Errors
Reply #2 - Apr 24th, 2009, 5:58pm
 
i don't think that expands works on arrays of arrays.
and println testing says so. although it doens't throw an exception the length doesn't increase to 2 but stays 1.
mmh expand seems just dont to work at all atmo
try to use PVector instead of x,y
Code:

class Tree {
 PVector[] points;
 int itCount;
 Tree(int x,int y,int itCountT) {
// dont forget this
   this.itCount=itCountT;
   points=new PVector[itCount+1];
   points[itCount] = new PVector(x,y);

//    println("v"+points.length);
 }
 void iterate() {
   itCount++;
   points = (PVector[]) append(points,new PVector(1,2));
....


ramin
Where does the error occur?
Reply #3 - Apr 24th, 2009, 6:54pm
 
It would be helpful it you gave some indication as to where the error occurred.

Without looking at the whole program (and not having any idea what it is supposed to do), the follow loop looks suspicious:

Code:
for(int i=0;i<3;i++) {
   points[itCount][i]=points[itCount-1][i]+int(random(0,3));
}


The variable i will have values 0, 1, 2... and points[itCount][2] doesn't exist.

All numbered array indices start at 0. An array with 2 elements, eg
Code:
int[] myArray = new int[2]; 


has only elements myArray[0] and myArray[1].

One handy "trick" (not really a trick) is to think of basic for loops as follows:
Code:
for(int i = 0; i < numberOfRepetitions; i++)
{
   // ...
}

Since in your case, you've used "3" in place of "numberOfRepetitions", which suggests you are processing 3 elements, not 2 (your "x" and "y").

In any case, you might want to reconsider some of your array initialisations in the form of new type[something + 1]. I haven't tried to understand what your code does, I'm just guessing you didn't realise that array indices start at 0 and go up to ONE LESS THAN the number of elements.

If you want 5 integers, new int[5] is what you should be using, not new int[5 + 1], and the elemnts are a[0], a[1], a[2], a[3], a[4] (supposing the array name is "a").

-spxl
Page Index Toggle Pages: 1