ArrayIndexOutOfBoundsException: 1024

Dear everyone;

when I compile a code in the processing. it has an error like this picture.


Please how to fix it.
Many thanks.!

1 Like

You have the screen 32x32 dimensions. 32*32 = 1024, hence the error. The for loop has the limit ~3000, therefore once you reach 1024 it is out of bounds. To fix it, you need to change the for loop to stop once it is no longer less than 1024 for(int i = 0; i < 1024; i++) {}

You can put in the for loop: for(int i = 0; i < img.width*img.height; i++) {} so it will never be out of bounds

Edit 2: Instead of posting the screenshot, I suggest you post the code in the text format (the </> button)
It makes the code readable and easy to copy!

//here is an example!
int abc = floor(PI*0.1234);
1 Like

@CodeMasterX

You were correct the first time.
He is saving canvas to an image and then loading that image which is the screen size in the keyPressed() function.

Some good tips in there can can still be used.

@babatnvn

You will only be able to use the local variables when keyPressed() is called.
This may be a consideration depending on what you are trying to do.

Look up variable scope:
VariableScope \ Examples \ Processing.org

Formatting code:
https://discourse.processing.org/faq#format-your-code

:)

1 Like

Didn’t notice! Thanks!

Thanks, everyone for reply. My picture has the size of 3072 (3x32x32). how to use the size() function for it?

do you mean RGB channels by 3x? Processing packs RGBA into a single value, so you only need to iterate for 32x32 times
https://processing.org/reference/pixels.html

Because in another program has for(int loop = 0 ; loop<3072 ; loop++ ){ img_buffer2[loop] = image_data[loop]; } and it calculated with 32x32x3 values.
So, if use size 32x32 in this program then Are these two values called equal? because this two program is related together.

Hello,

Some context is missing…

An integer array can store color as an integer.
A byte array can store individual bytes for R, G, B and A or a subset of these.

A few references to get you started:
saveBytes() \ Language (API) \ Processing 3+
loadBytes() \ Language (API) \ Processing 3+
Images and Pixels \ Processing.org
pixels[] \ Language (API) \ Processing 3+
Color \ Processing.org
color() \ Language (API) \ Processing 3+

:)