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 › array index out of bounds execption
Page Index Toggle Pages: 1
array index out of bounds execption (Read 1580 times)
array index out of bounds execption
Nov 13th, 2009, 7:12am
 
can't seem to figure out why i'm getting this error on line 56 any help would be great

Code:
import processing.video.*; //imports the processing video library

int lineloc = 1;
int[][] colors; // an array to store the color value of each pixel captured from the webcam
int signal = 0; // the location of the scanner represented by a white line that moves up and odwn the screen when a key is pressed
int dir = 1; // the direction that signal is traveling

Capture video; // declaring the video variable

void setup() {
 size(960,600);
 frameRate(60);
 colors = new int[height][width]; // initialization of colors array
 video = new Capture(this,width,height,60); // initializing the video variable
}

void draw(){
 background(0);

 // starting video capture
 if (video.available()) {
   video.read();
 }

 loadPixels(); //loading the pixels from the captured video into the default array video.pixels

   // the colors array is initialized here, with each array location getting a color value that
 // corresponds with each pixel in the captured video
 int t = 0;
 for(int i = 0; i < height;i++){
   for(int x = 0; x <width; x++){

     colors[i][x] = video.pixels[t];
     t++;
   }
 }


 if(mousePressed){
   signal = abs(mouseY%height);
 }
 else{
   signal += 0.3*dir;
 }
 
 signal += dir; // updating signal

 //checking to see if the signal line is at the top or bottom of the picture and reversing direction
 if(signal > height || signal < 0){
   dir *= -1;
 }
 
 //here is where the generative art is drawn
 if(!keyPressed){
     for(int x = 0; x <= width; x ++){
       stroke(colors[signal][x]);
       line(lineloc+x,0,lineloc+x,height);
   }
 }
 else{ // if any key is pressed it shows the webcam image and the current placement of the signal line
   loadPixels();
   image(video,0,0);
   stroke(255);
   line(0,signal,width,signal);
 }

}
Re: array index out of bounds execption
Reply #1 - Nov 13th, 2009, 7:25am
 
Code:

     for(int x = 0; x <= width; x ++){
       stroke(colors[signal][x]);
       line(lineloc+x,0,lineloc+x,height);
   }


the range of values for x will go 0 >> width whereas the array of colors it goes 0 >> width -1

Also note that signal goes 0 >> height -1 and since signal is a calculated value you might want to watch out for that as well.
Smiley
Re: array index out of bounds execption
Reply #2 - Nov 13th, 2009, 7:41am
 
thanks for that quark it helped me figure it out in just a couple of seconds. sometimes a fresh pair of eyes is all you need Smiley
Page Index Toggle Pages: 1