# ArrayOutOfBounds in bonified Shiffman tutorial

**URL:** <https://discourse.processing.org/t/arrayoutofbounds-in-bonified-shiffman-tutorial/21144>\
**Category:** Libraries\
**Created:** [May 21, 2020, 6:47pm UTC](https://discourse.processing.org/t/arrayoutofbounds-in-bonified-shiffman-tutorial/21144 "2020-05-21T18:47:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![clarbar9](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@clarbar9](https://discourse.processing.org/u/clarbar9)\
**Post date:** [May 21, 2020, 6:47pm UTC](https://discourse.processing.org/t/arrayoutofbounds-in-bonified-shiffman-tutorial/21144/1 "2020-05-21T18:47:54Z")

</div>

I was following Dan Shiffman’s [tutorial on Motion Detection](https://www.youtube.com/watch?v=QLHMtE5XsMs) 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](https://github.com/CodingTrain/website/blob/master/Tutorials/Processing/11_video/sketch_11_6_MotionDetection/sketch_11_6_MotionDetection.pde).

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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 21, 2020, 7:42pm UTC](https://discourse.processing.org/t/arrayoutofbounds-in-bonified-shiffman-tutorial/21144/2 "2020-05-21T19:42:08Z")

</div>

Hello,

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

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/905da5d08eac15c791745b277641710f4c014847.png)

Use the Force clarbar9:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c249e0518e9ee94f8d30c9dfd0113a526e3d145c.png)

`:)`

---

<div class="post-metadata">

**Author:** ![clarbar9](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@clarbar9](https://discourse.processing.org/u/clarbar9)\
**Post date:** [May 21, 2020, 7:56pm UTC](https://discourse.processing.org/t/arrayoutofbounds-in-bonified-shiffman-tutorial/21144/3 "2020-05-21T19:56:15Z")

</div>

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

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

```

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

```auto
[0] "FaceTime HD Camera"

```

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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 21, 2020, 8:01pm UTC](https://discourse.processing.org/t/arrayoutofbounds-in-bonified-shiffman-tutorial/21144/4 "2020-05-21T20:01:17Z")

</div>

Hello,

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

`:)`

---

<div class="post-metadata">

**Author:** ![clarbar9](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@clarbar9](https://discourse.processing.org/u/clarbar9)\
**Post date:** [May 21, 2020, 8:12pm UTC](https://discourse.processing.org/t/arrayoutofbounds-in-bonified-shiffman-tutorial/21144/5 "2020-05-21T20:12:52Z")

</div>

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:

```auto
color prevColor = prev.pixels[loc];

```

---

<div class="post-metadata">

**Author:** ![noahbuddy](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@noahbuddy](https://discourse.processing.org/u/noahbuddy)\
**Post date:** [May 22, 2020, 4:46am UTC](https://discourse.processing.org/t/arrayoutofbounds-in-bonified-shiffman-tutorial/21144/6 "2020-05-22T04:46:36Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![clarbar9](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@clarbar9](https://discourse.processing.org/u/clarbar9)\
**Post date:** [May 22, 2020, 9:55pm UTC](https://discourse.processing.org/t/arrayoutofbounds-in-bonified-shiffman-tutorial/21144/7 "2020-05-22T21:55:52Z")

</div>

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

```auto
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)](https://github.com/processing/processing-video/releases/tag/r6-v2.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 🙂
