# Array of PImages

**URL:** https://discourse.processing.org/t/array-of-pimages/46444
**Category:** Coding Questions
**Created:** [May 25, 2025, 2:50am UTC](https://discourse.processing.org/t/array-of-pimages/46444 "2025-05-25T02:50:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![nikapm](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/nikapm/32/20715_2.png) [@nikapm](https://discourse.processing.org/u/nikapm)
#### Post date: [May 25, 2025, 2:50am UTC](https://discourse.processing.org/t/array-of-pimages/46444/1 "2025-05-25T02:50:09Z")

</div>

Hello,

i am trying to build an array of images to be displayed i looping, but I receive the error message that image cannot be solve as variable.  
Here’s code:

```Processing
int maxImages = 10;
int imageIndex = 0;
PImage[] images = new PImage [maxImages];
void setup() {
  
  size(500, 555);
  
}

void draw(){
 for (int i = 0; i < images.length; i ++ ) {
    images[i] = loadImage( " i + 1 .jpg" );

image(images[imageIndex], 0, 0);
imageIndex = (imageIndex + 1)& image.length;

}
}

```

Can somebidy guive me a light?

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [May 25, 2025, 4:02am UTC](https://discourse.processing.org/t/array-of-pimages/46444/2 "2025-05-25T04:02:14Z")

</div>

```auto
1 PImage[] images = new PImage[maxImages]; 
2 images[i] = loadImage((i + 1) + ".jpg");
3 imageIndex = (imageIndex + 1) % images.length;
4 Move image loading to setup() 

```

---

<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 25, 2025, 4:37pm UTC](https://discourse.processing.org/t/array-of-pimages/46444/3 "2025-05-25T16:37:42Z")

</div>

Hello @nikapm ,

An important part of your journey in code development is using the tools you have to debug and work through your code.

Almost everything you need is here:  
_[https://processing.org/](https://processing.org/)_

Once you learn to navigate these it gets easier over time.

References:  
_[+ (addition) / Reference / Processing.org](https://processing.org/reference/addition.html)_  
[setup() / Reference / Processing.org](https://processing.org/reference/setup_.html) // Runs once!  
[draw() / Reference / Processing.org](https://processing.org/reference/draw_.html) // Loops at default framerate of 60 fps

> [@nikapm](#):
>
> `imageIndex = (imageIndex + 1)& image.length;`

This is not correct (there is no _image_ variable).  
If you move mouse over the error (red underline) it shows:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/4/c4680173d7ab7123ea0013e9cfcf105978295fac.png)

> [@nikapm](#):
>
> `images[i] = loadImage( " i + 1 .jpg" );`

There is no image named _" i + 1 .jpg"_ (everything inside quotes).  
Read this reference to see how to add an integer to a string:  
_[+ (addition) / Reference / Processing.org](https://processing.org/reference/addition.html)_

> [@nikapm](#):
>
> ```auto
> image(images[imageIndex], 0, 0);
> imageIndex = (imageIndex + 1)& image.length;
> 
> ```

The _imageIndex_ is _i_ in your _for()_ loop and you do not need a separate variable.

The reference for math operators is here and you will see what the & does:  
_[Reference / Processing.org](https://processing.org/reference/#math)_

You are displaying the images one after another in the _for()_ loop!  
Since the sketch window updates at the end of draw cycle you will only see the last image (once the code is corrected).

Use a _println()_ statements to help with debugging throughout your code.  
Reference:  
_[println() / Reference / Processing.org](https://processing.org/reference/println_.html)_

Code example and use of _println()_ to help debugging:

```auto
int maxImages = 10;
int imageIndex = 0;

PImage[] images = new PImage [maxImages];
  
for (int i = 0; i < maxImages; i ++ )
  {  
  // Important: Creates a String to use for image names
  String imgNum = str(i); // this could be i+1 if your image names are 1 to 10
  String imgExt = ".jpg";
  String imgName = imgNum + imgExt;
  
  // Shortcut for above:
  //String imgName = i + ".jpg"; // This casts i to a String before adding to a String
  
  println(imgName);
  
  images[i] = loadImage(imgName); 
  // This will give errors since I do not have images.
  // Console will show what is missing and that code used correct name!
  
  imageIndex = (imageIndex + 1) & maxImages; // You are using a bitwise & here!
  println(i, imageIndex); // See result of above math that is used in next loop.
  } 

```

The _PImage_ array indexes from 0 to 9 and you load images (I am assuming) from 1.jpg to 10.jpg.  
Add the 1 offset carefully in your code.

Have fun!

`:)`

---

<div class="post-metadata">

### Author: ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)
#### Post date: [May 25, 2025, 5:40pm UTC](https://discourse.processing.org/t/array-of-pimages/46444/4 "2025-05-25T17:40:23Z")

</div>

Should be

```auto
imageIndex = (imageIndex + 1 ) % maxImages;

```

using the modulo `%` operator rather than bit-wise `&` operator.

---

<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 27, 2025, 12:10am UTC](https://discourse.processing.org/t/array-of-pimages/46444/5 "2025-05-27T00:10:03Z")

</div>

> [@glv](#):
>
> The _imageIndex_ is _i_ in your _for()_ loop and you do not need a separate variable.

This should not be inside the for() loop!

I moved the bracket here:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/f/3f8e3067e4b75ab8a29fdb7e79ad66b62d6cb02b.png)

There have already been suggestions where to load images.  
Choose wisely and justify the choice.

`:)`
