# ArrayIndexOutOfBoundsException: 230400

**URL:** <https://discourse.processing.org/t/arrayindexoutofboundsexception-230400/4086>\
**Category:** Beginners\
**Created:** [October 3, 2018, 1:37pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-230400/4086 "2018-10-03T13:37:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdeepak](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mdeepak](https://discourse.processing.org/u/mdeepak)\
**Post date:** [October 3, 2018, 1:37pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-230400/4086/1 "2018-10-03T13:37:27Z")

</div>

Was trying to do metaballs in processing and got this error on line 3 pixels[index] = color(x,0,y). Don’t know what caused it or how to fix it. The code doesn’t look any different from Shiffman’s coding challenge #28.

 ![48%20AM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b0948e13b41c1a38a99a8ca94d7f915ab22e1298.png)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 3, 2018, 1:41pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-230400/4086/2 "2018-10-03T13:41:41Z")

</div>

Both `for (int y = 0; x < height; y++)` & `int index = x + y*width;` are wrong, given the _y_ coordinate is related to _height_ instead! 😬

---

<div class="post-metadata">

**Author:** ![mdeepak](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mdeepak](https://discourse.processing.org/u/mdeepak)\
**Post date:** [October 3, 2018, 1:54pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-230400/4086/3 "2018-10-03T13:54:35Z")

</div>

I was not paying attention. All I had to do was y = 0; y \< height; y++ instead I was doing y = 0; x \< height. It is working now. Thanks for the reply.

---

<div class="post-metadata">

**Author:** ![mdeepak](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mdeepak](https://discourse.processing.org/u/mdeepak)\
**Post date:** [October 3, 2018, 2:33pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-230400/4086/4 "2018-10-03T14:33:41Z")

</div>

I don’t think there’s anything wrong with index = x + y \* width though. What should be the right formula then? If you follow Shiffman’s youtube challenge, he goes through the derivation of the formula in processing tutorial(pixels in processing to be exact).

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 3, 2018, 3:21pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-230400/4086/5 "2018-10-03T15:21:25Z")

</div>

> [@mdeepak](#):
>
> I don’t think there’s anything wrong with `index = x + y * width` though.

Well, just try out something like `size(800, 600);` and see if it’s still gonna work for ya. 😜

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [October 3, 2018, 4:31pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-230400/4086/6 "2018-10-03T16:31:25Z")

</div>

You have it right 😉

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 3, 2018, 5:16pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-230400/4086/7 "2018-10-03T17:16:55Z")

</div>

Oops! W/ all that _width_ & _height_ confusion, I’ve got them all mixed up! 😵

The worst thing, I’ve done that formula so many times in my own example sketches! 🤡

Indeed, the _y_ coordinate has to be multiplied by _width_ in order to have the correct _index_ row within _pixels[]_: 🤦‍♂️

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [October 3, 2018, 5:31pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-230400/4086/8 "2018-10-03T17:31:33Z")

</div>

I had to look it several times too to be sure =p

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [October 4, 2018, 5:44pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-230400/4086/9 "2018-10-04T17:44:31Z")

</div>

If you ever forget the `y*width+x` form for a 2D index lookup, it is written on the reference page for `get()` – where they suggest using it instead!

> Getting the color of a single pixel with `get(x, y)` is easy, but not as fast as grabbing the data directly from `pixels[]` . The equivalent statement to `get(x, y)` using `pixels[]` is **`pixels[y*width+x]`**. [get() / Reference / Processing.org](https://processing.org/reference/get_.html)
