这个NullPointerException应该是什么意思?

时间:2014-03-01 00:56:34

标签: java nullpointerexception jmonkeyengine

这段代码给了我一个java.lang.NullPointerException,我不知道为什么。 确切的错误:

java.lang.NullPointerException
    at com.bminus.ttp.controller.HUD.bind(HUD.java:35)
    at de.lessvoid.nifty.screen.Screen.startScreen(Screen.java:210)
    at de.lessvoid.nifty.Nifty.gotoScreenInternal(Nifty.java:678)
    at de.lessvoid.nifty.Nifty.access$400(Nifty.java:77)
    at de.lessvoid.nifty.Nifty$1.perform(Nifty.java:635)
    at de.lessvoid.nifty.elements.EndOfFrameElementAction.perform(EndOfFrameElementAction.java:22)
    at de.lessvoid.nifty.Nifty.executeEndOfFrameElementActions(Nifty.java:439)
    at de.lessvoid.nifty.Nifty.handleDynamicElements(Nifty.java:358)
    at de.lessvoid.nifty.Nifty.update(Nifty.java:293)
    at com.jme3.niftygui.InputSystemJme.endInput(InputSystemJme.java:113)
    at com.jme3.input.InputManager.processQueue(InputManager.java:819)
    at com.jme3.input.InputManager.update(InputManager.java:883)
    at com.jme3.app.Application.update(Application.java:604)
    at com.bminus.ttp.ui.App.update(App.java:473)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
    at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
    at java.lang.Thread.run(Thread.java:744)

这是它指向的代码:

public void bind(Nifty theNifty, Screen theScreen)
  {
    this.nifty = theNifty;
    this.screen = theScreen;
    this.screen.addKeyboardInputHandler(new DefaultInputMapping(), this);
    final ConsoleControl control = (ConsoleControl)this.nifty.createPopup("console-popup").findControl("console", ConsoleControl.class);
    control.output("The Third Power");
    control.output("Type 'help()' to get list of all available commands.");
    control.addCommandHandler(new ConsoleCommandHandler()
    {
       public void execute(String theString)
      {
        try
        {
          Object result = HUD.SHELL.evaluate(String.format("TTP.%s", new Object[] { theString }));
          if (result != null) {
            if ((result instanceof Collection)) {
              for (Object resultItems : (Collection)result) {
                control.output(resultItems.toString());
              }
            } else {
              control.output(result.toString());
            }
          }
        }
        catch (Exception exception)
        {
          control.output(String.format("Unknown command: %s", new Object[] { theString }));
        }
      }
    });
    SHELL.setVariable("TTP", this.consoleAPI);
  }

更具体地说,这一行完全符合:

control.output("The Third Power");

我想知道的是为什么它给了我一个java.lang.NullPointerException以及如何修复此错误以及如何确保它永远不会回来。如果有人需要更多关于我想要的信息,那么就问问。谢谢!

3 个答案:

答案 0 :(得分:4)

变量controlnull,您尝试在其上调用方法output()。在尝试调用方法之前,您可能希望在null语句中检查if。如果您不希望它成为null,请尝试查看上一行返回null的原因。

final ConsoleControl control = (ConsoleControl)this.nifty.createPopup("console-popup").findControl("console", ConsoleControl.class);

答案 1 :(得分:1)

检查是否final ConsoleControl control ... 返回null

您可以添加语句if (control == null) {System.out.println("I found the problem, now I have to figure out why control is null);}

答案 2 :(得分:1)

从您的代码中,findControl方法正在返回null,这会导致此nullpointer异常,因为它使控件为null。只是疯狂狂野的猜测,请忽略它,如果它不正确。你期望在findControl方法中传递“console”,还是应该与“console-popup”相同。有时我们会输入错误并查看不同的方向。

无论如何只要完成答案,你可以在调用find control方法后添加这一行。

final ConsoleControl control = (ConsoleControl)this.nifty.createPopup("console-popup").findControl("console", ConsoleControl.class);
if(control == null){
//log error message if want
return;
}