# ArrayIndexOutOfBoundsException: 1024

**URL:** <https://discourse.processing.org/t/arrayindexoutofboundsexception-1024/28522>\
**Category:** Coding Questions\
**Created:** [March 14, 2021, 8:50am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-1024/28522 "2021-03-14T08:50:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![babatnvn](https://avatars.discourse-cdn.com/v4/letter/b/c89c15/32.png) [@babatnvn](https://discourse.processing.org/u/babatnvn)\
**Post date:** [March 14, 2021, 8:50am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-1024/28522/1 "2021-03-14T08:50:31Z")

</div>

Dear everyone;

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

 ![Capture](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0d06b0954bd580ec2e91c195b2e06ccfcb30bfdd.jpeg)  
Please how to fix it.  
Many thanks.!

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [March 14, 2021, 9:41am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-1024/28522/2 "2021-03-14T09:41:14Z")

</div>

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!

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

```

---

<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:** [March 14, 2021, 10:53am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-1024/28522/3 "2021-03-14T10:53:10Z")

</div>

@CodeMasterX

> [@CodeMasterX](#):
>
> Edit: I didn’t notice it was a loaded image.

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:  
_[Variable Scope / Examples / Processing.org](https://processing.org/examples/variablescope.html)_

Formatting code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [March 14, 2021, 10:55am UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-1024/28522/4 "2021-03-14T10:55:51Z")

</div>

Didn’t notice! Thanks!

---

<div class="post-metadata">

**Author:** ![babatnvn](https://avatars.discourse-cdn.com/v4/letter/b/c89c15/32.png) [@babatnvn](https://discourse.processing.org/u/babatnvn)\
**Post date:** [March 14, 2021, 2:02pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-1024/28522/5 "2021-03-14T14:02:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [March 14, 2021, 2:09pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-1024/28522/6 "2021-03-14T14:09:37Z")

</div>

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](https://processing.org/reference/pixels.html)

---

<div class="post-metadata">

**Author:** ![babatnvn](https://avatars.discourse-cdn.com/v4/letter/b/c89c15/32.png) [@babatnvn](https://discourse.processing.org/u/babatnvn)\
**Post date:** [March 14, 2021, 3:09pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-1024/28522/7 "2021-03-14T15:09:28Z")

</div>

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.

---

<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:** [March 14, 2021, 3:24pm UTC](https://discourse.processing.org/t/arrayindexoutofboundsexception-1024/28522/8 "2021-03-14T15:24:28Z")

</div>

Hello,

> [@babatnvn](#):
>
> Because in another program has

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() / Reference / Processing.org](https://processing.org/reference/saveBytes_.html)_  
_[loadBytes() / Reference / Processing.org](https://processing.org/reference/loadBytes_.html)_  
_[https://processing.org/tutorials/pixels/](https://processing.org/tutorials/pixels/)_  
_[pixels[] / Reference / Processing.org](https://processing.org/reference/pixels.html)_  
_[Color / Processing.org](https://processing.org/tutorials/color/)_  
_[color() / Reference / Processing.org](https://processing.org/reference/color_.html)_

`:)`
