NullPointer - TextureRegions

So, here we have a 96x64 image, composed of 6 32x32 sprites.

Alright, so I have this loading and drawing to the screen (just to make sure it’s not a problem with loading the texture)

So, here’s the line of code throwing the null-pointer

		regions[0][0] = new TextureRegion(playerSheet, 0, 0, 32, 32); 

This, from what I understand is supposed to get the first 32x32 pixels starting from 0, 0 in the image, and save it to a TextureRegion.

	private TextureRegion[][] regions;

However, It’s throwing a null and I’m not sure why.

Exception in thread "LWJGL Application" java.lang.NullPointerException
	at com.chris.platformer.entities.Player.loadAssets(Player.java:64)
	at com.chris.platformer.entities.Player.<init>(Player.java:33)
	at com.chris.platformer.screens.GameScreen.show(GameScreen.java:25)
	at com.badlogic.gdx.Game.setScreen(Game.java:62)
	at com.chris.platformer.Game.create(Game.java:10)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

Here’s my loadAssets() method

	private void loadAssets()
	{	
		regions[0][0] = new TextureRegion(playerSheet, 0, 0, 32, 32); 
		regions[1][0] = new TextureRegion(playerSheet, 32, 0, 32, 32); 
		regions[2][0] = new TextureRegion(playerSheet, 64, 0, 32, 32); 
		regions[3][0] = new TextureRegion(playerSheet, 0, 32, 32, 32);	
		regions[4][0] = new TextureRegion(playerSheet, 32, 32, 32, 32);
		regions[5][0] = new TextureRegion(playerSheet, 64, 32, 32, 32);
		
		walkLeft = new Animation(0.40f, regions[0][0], regions[1][0], regions[2][0]);
		walkRight = new Animation(0.40f, regions[3][0], regions[4][0], regions[5][0]);
		
		playerSprite = new Texture("graphics/sprites/player/Player.png");
	}

I believe that should get all 6 of the images, however it can’t even get the first one.

You have 2 possible variables that could be causing the problem

first, either


playerSheet

or


regions[][]

may be null.

Check to see if your playerSheet is null, as I don’t see that anywhere in your snippets you posted

you could use this


if(playerSheet == null){
System.out.println("playerSheet is null");
}

for the array of TextureRegions

make sure you initialize the array


private TextureRegion[][] regions;

public Player(){
regions = new TextureRegion[size][size];
}

edit: renamed the constructor so people don’t have to read the non code snippets.

The exception is not happening inside the TextureRegion constructor so it must be the regions array that is null.
Can you show us how you’re initialising that array?

if you re read it, you will know I never said TextureRegion constructor, but the TextureRegion region[][] is null, I showed him how to initialize the array, because either region[][] or playerSheet is null as they are the only variables that can produce a null pointer, like I said in my post that you only read the code snippets of.

To remove your confusion, I will edit the code and use his class name that he had the exception in.

Phased hit the nail on the head, I happen to forget to initialize arrays frequently and it never registers to me when I recieve the error while trying to set the values