ArrayIndexOutOfBoundsException: 230400

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.

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

2 Likes

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.

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

1 Like

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

1 Like

You have it right :wink:

Oops! W/ all that width & height confusion, I’ve got them all mixed up! :dizzy_face:

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

Indeed, the y coordinate has to be multiplied by width in order to have the correct index row within pixels[]: :man_facepalming:

1 Like

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

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

2 Likes