我在游戏中添加了一个新参数(libGDX),我得到了一个奇怪的错误:
Exception in thread "main" java.lang.NullPointerException
at com.game.game.GameLaunching.<init>(GameLaunching.java:21)
at com.game.game.Main.main(Main.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
当我尝试使用 thm 时出现此错误。使用其他参数一切正常。 代码:
public class GameLaunching extends Game
{
public boolean isPause = false;
public boolean music, sound;
public String flag;
public String thm;
public int scoreForShop;
public boolean isFailture;
public GameLaunching(String flag, String thm, boolean music, boolean sound)
{
Gdx.app.info("1", thm); // error
this.flag = flag;
this.music = music;
this.sound = sound;
}
@Override
public void create()
{
isPause = false;
setScreen(new GameSurface(this));
}
主类代码:
public static void main(String[] args)
{
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "Game";
cfg.width = 800;
cfg.height = 480;
new LwjglApplication(new GameLaunching("8","night", false, true), cfg);
}
我该如何解决?
答案 0 :(得分:2)
您应该在主要方法
中的Gdx.app.info("1", thm); // error
旁边移动new LwjglApplication(new GameLaunching("8","night", false, true), cfg);
这一行
原因在Libgdx中,库的初始化直到create方法开始之前才完成(Libgdx基础结构将调用它来指示您的应用程序已启动),因此您无法访问Gdx 。直到那时。