NullPointerException when loading an image

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

1 Like

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

blocks=new PImage[....

1 Like

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

1 Like