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 › arrayindexoutofbounds PLEASE help
Page Index Toggle Pages: 1
arrayindexoutofbounds PLEASE help (Read 1104 times)
arrayindexoutofbounds PLEASE help
Sep 5th, 2007, 11:15pm
 
everytime i call an image from my array i get a arrayindexoutofbounds:#. this happens 5 minutes after i started the app. why?

thankyou
Slm
Re: arrayindexoutofbounds PLEASE help
Reply #1 - Sep 5th, 2007, 11:33pm
 
Hi,

Sounds like a loop problem.

You'll get a better/faster help by posting your code to see where is your problem. Wink
Re: arrayindexoutofbounds PLEASE help
Reply #2 - Sep 5th, 2007, 11:40pm
 
import maxlink.*;
import processing.opengl.*;
import pitaru.sonia_v2_9.*;

PImage pixel;
PImage[] myImages;  
int currentImage = 1;  
MaxLink link = new MaxLink(this, "beat_detection");

void setup()
{

 pixel = loadImage( "gun.gif");
 Sonia.start(this);
 LiveInput.start(128);
 size(800, 471, OPENGL);
 myImages = new PImage[2];
 // load images
 myImages[0] = loadImage( "pibe.gif" );
 myImages[1] = loadImage( "gun.gif" );
 link.declareMaxFunction("beatimage");
 colorMode(RGB,255,255,255,100);  
 frame.setLocation(100,screen.height/8);

}

public void beatimage()    

{
 currentImage++;
 currentImage %= myImages.length;
}

void draw(){

 hello();

}

void hello()
{
 float input = LiveInput.getLevel()*height*2;  
 int cellsize = int(input);
 int COLS, ROWS;
 if(cellsize < 13){
   cellsize = int(random(13,15));
 }
 frameRate(80);
 COLS = myImages[currentImage].width/cellsize;
 ROWS = myImages[currentImage].height/cellsize;
 
 loadPixels();
 imageMode(CORNER);
 background(0);
 for ( int i = 0; i < COLS;i++) {
   for ( int j = 0; j < ROWS;j++) {
     int x = i*cellsize + cellsize/2;
     int y = j*cellsize + cellsize/2;
     int loc = x + y*myImages[currentImage].width;



// THIS IS THE LINE WHICH IS MARKED WHEN IT CHRASHES      
float z = 5* brightness(myImages[currentImage].pixels[loc]) - 100.0f;  
// THIS IS THE LINE WHICH IS MARKED WHEN IT CHRASHES  


     pushMatrix();
     translate(x,y,z);
     fill(int(random(0,255)),int(random(0,255)),int(random(0,255)),100);
     rectMode(CENTER);

     // shape
     beginShape();
     vertex(0, 15);
     vertex(30, 15);
     vertex(30, 25);
     vertex(0, 25);
     endShape();

     smooth();
     noStroke();
     beginShape();
     vertex(10, 0);
     vertex(20, 0);
     vertex(20, 60);
     vertex(10, 60);
     endShape();

     popMatrix();
     
   }
 }
}


public void init() {
 frame.setUndecorated(true);
 super.init();
}


public void stop(){
 Sonia.stop();
 super.stop();
}
Re: arrayindexoutofbounds PLEASE help
Reply #3 - Sep 19th, 2007, 2:07pm
 
there are a couple problems with this code.

1) i suspect your problem is caused by the beatimage() function. there will be some cases where currentImage, for a fraction of a second, will be '2', before the %= line happens. change to:
Code:
public void beatimage() {
currentImage = (currentImage + 1) % myImages.length;
}


2) as stated in the reference, size() must go first inside setup(). otherwise all the code before it will run twice.

3) you must call loadPixels() on any image for which you want to use the pixels array. this is also in the reference for pixels[].

4) if these things don't work, break up your "float z =" line to figure out which array is giving you the exception.
Page Index Toggle Pages: 1