We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am using Processing 2.0.3 on a 15" MacBook Pro running Mac OS X 10.8.5.
I am getting an "ArrayIndexOutOfBoundsException: -1" error when running the following sketch from Dan Shiffman's Learning Processing text pp. 284-5.
The only thing I have changed from his code is the "Construct the Capture Object" code for V 2.0.3.
This is the line that is flagged with the error message:
color c = video.pixels[loc];
Here's the sketch:
import processing.video.*;
int videoScale = 10;
int cols, rows;
Capture video;
void setup() {
size(640, 480);
cols = width/videoScale;
rows = height/videoScale;
smooth();
// Construct the Capture object
String[] cameras = Capture.list();
video = new Capture(this, cameras[12]); // 640 x 480 x 30 fps Fire-i cam
video.start();
}
void draw() {
if (video.available()) {
video.read();
}
background(0);
video.loadPixels();
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
// Where are we, pixel-wise?
int x = i*videoScale;
int y = j*videoScale;
int loc = (video.width - i - 1) + j * video.width;
color c = video.pixels[loc]; // Flagged ArrayIndexOutOfBoundsException: -1
float sz = (brightness(c)/255.0)*videoScale;
rectMode(CENTER);
fill(255);
noStroke();
rect(x + videoScale/2, y + videoScale/2, sz, sz);
}
}
}
I cannot see why I am getting this error. Can you help?
Thanks,
George Roland
Answers
what happens if line 18 is false? the block underneath it will still run but video. width will be 0, local in 28 will be -1, that's causing your error