【问题标题】:What is this NullPointerException supposed to mean?这个 NullPointerException 应该是什么意思?
【发布时间】:2014-03-01 00:56:33
【问题描述】:

这段代码给了我一个 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 以及我如何修复这个错误以及如何确保它永远不会回来。如果有人需要更多关于我想要什么的信息,那就问吧。谢谢!

【问题讨论】:

  • 你在 Eclipse 中尝试过调试模式吗?如果没有,您可能需要下载 Eclipse 集成开发环境 .. 它将为您提供相当多的故障排除(和编写)帮助!
  • 这意味着您正在尝试对不存在的对象执行某些操作。也许它从未被实例化。
  • 我正在使用 jMonkey Engine (NetBeans),我必须坚持下去。 @Erstwhilellll
  • @leigero 我该怎么做?

标签: java nullpointerexception jmonkeyengine


【解决方案1】:

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

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

【讨论】:

  • 这可能有点令人讨厌,但为什么它会返回 null 以及如何检查它是否返回?
  • 错误堆栈显示at com.bminus.ttp.controller.HUD.bind(HUD.java:35)。检查是否是同一行。
  • 这是他指定的行下面的行。 @Ravinder
  • 是的。行34 导致null 输出control 变量。因此,NPE 位于35
  • 我真的不知道你正在使用这个 API。我相信您的话,HUD.java 第 35 行是您在帖子中提到的 control.output(...) 行。这意味着您调用的findControl() 方法将返回null。我会查阅该方法的 API 文档,但我怀疑它找不到任何名为“控制台”的控件,类型为 ConsoleControl.class
【解决方案2】:

从您的代码中,findControl 方法正在返回 null,这会导致此 nullpointer 异常,因为它使控件为 null。只是疯狂的疯狂猜测,如果不正确,请忽略它。您是否希望在 findControl 方法中传递“控制台”,或者它应该与“控制台弹出”相同。有时我们确实会输入错误并寻找不同的方向。

无论如何只是为了完成答案,您可以在调用 find Control 方法后添加此行。

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

【讨论】:

  • 当我通过漂亮项目中的 ConsoleConntrol.class 时,我将暂时执行此操作。谢谢!
【解决方案3】:

检查是否final ConsoleControl control ... 返回一个空值

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

【讨论】:

    猜你喜欢
    • 2011-04-11
    • 2018-10-29
    • 1970-01-01
    • 2017-05-25
    • 2010-10-03
    • 2013-02-21
    • 2015-01-09
    • 2011-10-12
    • 2015-06-22
    相关资源
    最近更新 更多