Libgdx:Singleton上无法解释的NullPointerException

Chr*_*her -3 java libgdx

可能只对libGDX技能负责.

MyGame.java:

public void create() { // gets called after constructor
        Resources.setInstance(new Resources());
}
Run Code Online (Sandbox Code Playgroud)

Resources.java

public static void setInstance(Resources res) {
        if (instance != null) {
            instance = res;
        }
        else throw new IllegalStateException(
                "instance cant be null");
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

Exception in thread "LWJGL Application" java.lang.IllegalStateException: instance cant be null
    at com.mutant.td.Resources.setInstance(Resources.java:22)
    at com.mutant.td.SpeciesWarGame.create(SpeciesWarGame.java:41)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Run Code Online (Sandbox Code Playgroud)

我真的不知道发生了什么.

Kon*_*Kon 5

这是错的:

 if (instance != null)
Run Code Online (Sandbox Code Playgroud)

你在这里检查错了.你应该检查:

 if (res != null)
Run Code Online (Sandbox Code Playgroud)

因为这是你传递给你的方法的东西.