# ArrayOutOfBoundException for pixels

**URL:** <https://discourse.processing.org/t/arrayoutofboundexception-for-pixels/30214>\
**Category:** Coding Questions\
**Created:** [May 21, 2021, 6:59pm UTC](https://discourse.processing.org/t/arrayoutofboundexception-for-pixels/30214 "2021-05-21T18:59:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![res56](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@res56](https://discourse.processing.org/u/res56)\
**Post date:** [May 21, 2021, 6:59pm UTC](https://discourse.processing.org/t/arrayoutofboundexception-for-pixels/30214/1 "2021-05-21T18:59:56Z")

</div>

Why do I get an ArrayOutOfBoundException for this code?

void setup(){  
size(400,400);  
}

void draw(){  
background(44);  
loadPixels();

for(int x=0; x \< width; x++){  
for(int y=0; y \< height; y++){  
int index=(x+y\*width)\*4;  
pixels[index]=color(255, 102, 204);  
}  
}

updatePixels();  
}

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 21, 2021, 7:03pm UTC](https://discourse.processing.org/t/arrayoutofboundexception-for-pixels/30214/2 "2021-05-21T19:03:16Z")

</div>

` int index=(x+y*width)*4;`

The problem is the `* 4` bit !!!

---

<div class="post-metadata">

**Author:** ![res56](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@res56](https://discourse.processing.org/u/res56)\
**Post date:** [May 23, 2021, 7:12am UTC](https://discourse.processing.org/t/arrayoutofboundexception-for-pixels/30214/3 "2021-05-23T07:12:30Z")

</div>

Thanks! You saved my life 🙂

---

<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 23, 2021, 10:00am UTC](https://discourse.processing.org/t/arrayoutofboundexception-for-pixels/30214/4 "2021-05-23T10:00:36Z")

</div>

Hello,

There is an example and discussion of this (without error) in this tutorial:  
_[Images and Pixels \ Processing.org](https://processing.org/tutorials/pixels/)_

Also a discussion of the error (and more) here:

- [https://forum.processing.org/two/discussion/8093/frequently-asked-questions](https://forum.processing.org/two/discussion/8093/frequently-asked-questions)(See Exceptions)
- [Home · processing/processing Wiki · GitHub](https://github.com/processing/processing/wiki)(See Common Errors)

Example using _println()_ to show what happened:

```auto
void setup() 
  {
  size(400, 400);
  }

void draw() 
  {
  background(44);
  loadPixels();
  println(pixels.length); //added

  for (int x=0; x < width; x++) 
    {
    for (int y=0; y < height; y++) 
      {
      int index=(x+y*width)*4;
      if (index >= pixels.length-1) println(index,"Woops!"); //added
      pixels[index]=color(255, 102, 204);
      }
    }
  updatePixels();
  }

```

`:)`
