ArrayOutOfBounds in bonified Shiffman tutorial

I was following Dan Shiffman’s tutorial on Motion Detection and kept getting an ArrayOutOfBounds error. I couldn’t figure out why so I decided to test the exact same code copied directly from the Coding Train github account.

Lo and behold, I’m getting the exact same error: ArrayIndexOutOfBoundsException: 230400 . Particularly the editor highlights line 60 color prevColor = prev.pixels[loc];

I’ve tried using the debugging console to verify the values of variables, but can’t see why there would be any issues. I should mention that this exact same code seems to work for Shiffman in the Processing tutorial. Any idea why this would be the case?

Thanks in advance for the help! x

1 Like


It works here.
The first thing I had to do was select the correct camera.

Use the Force clarbar9:


1 Like

Hey glv, thanks for the reply! Ooo I should have mentioned the first time around that I did change the selected camera:

video = new Capture(this, cameras[0]);

and when I ran the code the console does print out:

[0] "FaceTime HD Camera"

But I’m still getting the same error :confused:
Any clue?

1 Like


It works here so can’t offer more help at this time.

You only had that one choice for cam without any details?
I had to scroll through to find 640x360 at 30 fps


Well that is interesting! Indeed I only have one choice of camera and I didn’t get any further details. However, that camera is definitely being accessed because the green light turns on.

I’ve used this same line of code in a different sketch where the camera is accessed, the camera name is printed to the console and I get no errors.

My hunch is that it has something to do with this line of code:

color prevColor = prev.pixels[loc];
1 Like

Based on the number you are getting, it seems your video input is at a higher resolution than your preview image. The number 230400 is exactly divisible by 360 and results in 640.
That means the value in loc is exceeding the size of prev.pixels.

What happens if you use the following inside setup() to initialize `prev’?

prev = createImage(video.width, video.height, RGB);

Alternatively, step through the width and height of prev in the dual for-loops instead of the dimensions of video.

1 Like

Ah, I see what happened now. The parameters for Capture were wrong. I’ve changed them to include width and height.

video = new Capture(this, 640, 360, cameras[0]);

Now it works like a charm!

This may have something to do with the fact that I’m using the
Video Library Release 6 (version 2.0-beta4) library rather than the standard video library one gets when they ‘import library’ through the processing editor.

In any case, thanks for the help everyone! Your advice was motivating nonetheless :slight_smile: