简体   繁体   English

即使已加载我的文件(Android Studio,libGDX,Java),AssetManager.get()也返回null

[英]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. 我正在使用libGDX的AssetManager,已检查是否已使用.update()方法加载了文件。 Note that my boolean loaded is true once AssetManager.update() returns true (which means everything has been loaded). 请注意,一旦AssetManager.update()返回true(这意味着所有内容均已加载),我的布尔值加载为true。

if(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) 这是我的设置(在我的WallList构造函数中)

//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. 在上面的代码中,我创建了一个HashMap并分配了“ sqr_wall_red”,这也是png的名称。 I then call TextureFromColor to get the Texture from the AssetManager while using Color.Red as parameter. 然后,我使用Color.Red作为参数,调用TextureFromColor从AssetManager获取纹理。

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) 线程“ LWJGL应用程序”中的异常com.cedric.game.geometry.WallList.getTextureFromColor(WallList.java:45)处com.cedric.game.geometry.WallList。(WallList.java:39)处的java.lang.NullPointerException

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() 并且如果我打印我用于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() 我将其作为输出(与assetManager.load()中的确切输入匹配)

data/Sprites/sqr_wall_red.png 数据/精灵/ sqr_wall_red.png

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 . 该代码行中可能存在3个可能导致NPE的位置: gamegetAssetManager()colors You need to determine which one it is. 您需要确定它是哪一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM