Cedric Martens Cedric Martens - 8 months ago 49
Android Question

AssetManager.get() returns null even if my file has been loaded (Android Studio, libGDX, Java)

I'm using libGDX's AssetManager, I have checked that my files have been loaded using the .update() method. Note that my boolean loaded is true once AssetManager.update() returns true (which means everything has been loaded).

if(player == null && walls == null)
player = new Player(this);
walls = new WallList(this);
//unrelated stuff

My player object uses a texture and it works just fine.
But my walls object also uses a texture but it crashes. Here's my setup (in my WallList constructor)

//unrelated stuff
this.colors = new HashMap<Color, String>();
this.spawningColor = Color.red;

colors.put(Color.red, "sqr_wall_red");

for(int i = 0; i < wallCountNeeded; i++)
this.add(new Wall(getTextureFromColor(spawningColor), i*wallWidth, 0));

In the code above, I make a HashMap and assign "sqr_wall_red" which is also the name of the png. I then call TextureFromColor to get the Texture from the AssetManager while using Color.Red as parameter.

private Texture getTextureFromColor(Color color)
return game.getAssetManager().get("data/Sprites/" + colors.get(color) + ".png", Texture.class);

And I get this error

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.cedric.game.geometry.WallList.getTextureFromColor(WallList.java:45)
at com.cedric.game.geometry.WallList.(WallList.java:39)

I'm pretty confident in the fact that the path is alright since I load it like this

assetManager.load("data/Sprites/sqr_wall_red.png", Texture.class);

and if I print the path I'm using for assetManager.get()

System.out.println("data/Sprites/" + colors.get(spawningColor) + ".png");

I get this as an output (which matches the exact input in the assetManager.load()


I think I have provided sufficient information in order to resolve my issue, but if you need more I will gladly show more.

return game.getAssetManager().get("data/Sprites/" + colors.get(color) + ".png", Texture.class);

There are 3 possible places in that line of code that can cause an NPE: game, getAssetManager(), and colors. You need to determine which one it is.