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 › need help with index out of bounds exception
Page Index Toggle Pages: 1
need help with index out of bounds exception (Read 729 times)
need help with index out of bounds exception
Oct 20th, 2009, 11:42am
 
so i keep getting an index out of bounds exception and i'm not sure why. i'm pretty sure that my arraylists have data in them. the problem is in the function void detectCollision at the bottom. any help would be great.

Edit: i added the orb class so the code should run if you try it now.

Code:
import ddf.minim.*;

int orbsize = 50;
int orbcount = 50;

AudioSample gong;
Minim minim;

Orb[] orbs = new Orb[orbcount];
float[] orbsx = new float[orbcount];
float[] orbsy = new float[orbcount];

boolean once = true;

int[] xtrail = new int[orbsize];
int[] ytrail = new int[orbsize];

ArrayList[] detcolx = new ArrayList[orbcount];
ArrayList[] detcoly = new ArrayList[orbcount];

void setup(){
 size(960,600);
 smooth();

 for(int i = 0; i < orbs.length; i++){
   orbsx[i] = random(25,935);
   orbsy[i] = random(25,575);
   orbs[i] = new Orb(orbsx[i],orbsy[i],orbsize);
 }

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

 

 minim = new Minim(this);
 gong = minim.loadSample("gong_d4.wav");


 checkoverlap();
}
void draw(){

 background(0);
 for(int i = 0; i < detcolx.length; i++){
   detcolx[i] = new ArrayList();
   detcoly[i] = new ArrayList();
 }


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

 checkoverlap();


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

   if(mouseX > orbs[i].orbx-orbs[i].orbdia/2 && mouseX < orbs[i].orbx+orbs[i].orbdia/2 && mouseY > orbs[i].orby-orbs[i].orbdia/2 && mouseY < orbs[i].orby+orbs[i].orbdia/2 && mousePressed){
     orbs[i].orbx = mouseX;
     orbs[i].orby = mouseY;
     for(int n = 0; n < 360; n++){
       detcolx[i].add(orbs[i].orbx + orbs[i].orbdia*cos(n));
       detcoly[i].add(orbs[i].orby + orbs[i].orbdia*sin(n));
       //println(n + ". " + detcolx[i].get(n) + ", " + detcoly[i].get(n));
     }
     stroke(255);
     fill(0);
     
     for (int x = 0; x < xtrail.length-1; x ++ ) {
       xtrail[x] = xtrail[x+1];
       ytrail[x] = ytrail[x+1];
     }
     xtrail[xtrail.length-1] = mouseX;
     ytrail[ytrail.length-1] = mouseY;

     for (int t = 0; t < xtrail.length; t ++ ) {
       ellipse(xtrail[t],ytrail[t],t,t);
     }

     orbs[i].mouse = true;
     if(once == true){
       gong.trigger();
       once = false;
     }
     //player.rewind();
     detectCollision();
   }
   else if(!mousePressed){
     orbs[i].mouse = false;
     //minim.stop();
     //player.cue(0);
     //player = minim.loadFile("gong_d4.wav");
     //      if(once == true){
     //        player = minim.loadFile("gong_d4.wav");
     //        once = false;
     //      }
   }
 }
 
}

class Orb{
 float orbx;
 float orby;
 float orbdia;
 boolean mouse;
 
 Orb(float x, float y, float dia){
   orbx = x;
   orby = y;
   orbdia = dia;
   mouse = false;

 }

 void display(){
   if(mouse == false){
     stroke(100,100,100);
   }
   else if(mouse == true){
     stroke(255);
   }
   strokeWeight(2);
   fill(0);
   ellipseMode(CENTER);
   ellipse(orbx, orby, 50,50);
   
 }
 
}

void checkoverlap(){
 int temp = 0;
 for(int i = 0; i < orbsx.length; i++){
   for(int x = 1; x < orbsx.length; x++){
     if((sqrt(sq(orbs[i].orbx-orbs[x].orbx)+sq(orbs[i].orby-orbs[x].orby))) < 50){

       orbs[i].orbx+=2;
       orbs[i].orby+=2;
       orbs[x].orbx-=2;
       orbs[x].orby-=2;
       //orbs[x].mouse = true;
       //player.play();
       //player.rewind();

     }
   }
 }
}

void mouseReleased(){
 gong.close();
 minim.stop();
 once = true;
 
}



void detectCollision(){
 for(int i = 0; i < detcolx.length; i++){
   for(int x = 1; x < detcolx.length; x ++){
     for(int n = 0; n < 360; n++){
       for(int t = 0; t < 360; t++){
         if(detcolx[i].get(n) == detcolx[x].get(t) && detcoly[i].get(n) == detcoly[i].get(t)){
           println("Success!");
         }
       }
     }
   }
 }
}
Re: need help with index out of bounds exception
Reply #1 - Oct 20th, 2009, 4:27pm
 
First exception I hit was a nullPointer due to lack of a gong.wav.  Commented out the sound bits and hit the outOfBounds...

I think the problem is that you are mixing array[] access and ArrayList objects.  I think all you need in this example is an array, since you have a defined, constant number of objects.  I'm not sure why you would need each orb to have a flexible list of its collision status with every other orb...especially since you reinitialize the lists each frame.

The specific reason for the exception is the detcolx.get(n), with an n running up to 360.  But detcolx will be empty unless it is filled using a loop of detcolx[i].add() .. which only happens during a specific mouse condition.  You can change those 360s to "detcolx[i].size()" to avoid the range issues...but the design is still going to be hairy to work with, I would think.

continuation:
http://processing.org/discourse/yabb2/num_1256086458.html
Page Index Toggle Pages: 1