# NullPointerException when loading an image

**URL:** <https://discourse.processing.org/t/nullpointerexception-when-loading-an-image/36125>\
**Category:** Coding Questions\
**Created:** [April 5, 2022, 10:11am UTC](https://discourse.processing.org/t/nullpointerexception-when-loading-an-image/36125 "2022-04-05T10:11:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jokpau](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@Jokpau](https://discourse.processing.org/u/Jokpau)\
**Post date:** [April 5, 2022, 10:11am UTC](https://discourse.processing.org/t/nullpointerexception-when-loading-an-image/36125/1 "2022-04-05T10:11:18Z")

</div>

Hello, I am trying to make a program for which I want to load images with an array. I tried a few variants, but they return a null pointer exception when ran.  
This is the main code of the program:

> AllBlocks allBlocks;
> 
> int blockNumber = 1;
> 
> void setup(){  
> fullScreen();  
> background (255);  
> allBlocks = new AllBlocks(blockNumber);  
> }

Then I made two variants of the AllBlocks class, but both don’t work.  
Variant 1:

> class AllBlocks{  
> //declare class variables  
> PImage blocks;  
> int blockNumber;

> AllBlocks(int tempBlockNumber){  
> blockNumber = tempBlockNumber;  
> for (int count = 0; count \< blockNumber; count++){  
> String name = str(count) + “.png”;  
> println(name);  
> println(count);  
> blocks[count] = loadImage(name);  
> }  
> }  
> }

The second variant:

> //display all available blocks  
> class AllBlocks{  
> PImage blocks;  
> String names;  
> int blockNumber;  
> PImage test;  
> AllBlocks(int tempBlockNumber){  
> blockNumber = tempBlockNumber;  
> //int count = 1;  
> names = new String[blockNumber];  
> for (int count = 0; count \< names.length; count++){  
> names[count] = str(count) + “.png”;  
> }  
> //names = {“1.png”)  
> for (int count = 0; count \< names.length; count++){  
> String imgNames = names[count];  
> println(imgNames);  
> println(count);  
> //blocks[count] = loadImage (“1.png”);  
> blocks[count] = loadImage (imgNames);  
> }  
> //test = loadImage (“1.png”);  
> }  
> }

Thank you in advance to anyone who will try to help me.  
Jokpau

---

<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:** [April 5, 2022, 10:39am UTC](https://discourse.processing.org/t/nullpointerexception-when-loading-an-image/36125/2 "2022-04-05T10:39:40Z")

</div>

> [@Jokpau](#):
>
> names = new String[blockNumber];

I think that you have to do the same thing with `blocks`  
prior to filling it

`blocks=new PImage[....`

---

<div class="post-metadata">

**Author:** ![Jokpau](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@Jokpau](https://discourse.processing.org/u/Jokpau)\
**Post date:** [April 5, 2022, 11:29am UTC](https://discourse.processing.org/t/nullpointerexception-when-loading-an-image/36125/3 "2022-04-05T11:29:36Z")

</div>

Thank you! It is always something simple that is overlooked.
