Search code examples
javanullpointerexceptionjmonkeyengine

What is this NullPointerException supposed to mean?


This code is giving me a java.lang.NullPointerException and I don't know why. Exact error:

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)

And here is the code it is pointing to:

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);
  }

More specifically, this line exactly:

control.output("The Third Power");

What I want to know is why it is giving me a java.lang.NullPointerException and how I can fix this error and also how to make sure it never comes back. If anyone needs more information about what I want then just ask. Thank you!


Solution

  • From your code findControl method is returing null which is causing this nullpointer exception as it makes control null. Just wild wild wild guess and please ignore this if its not correct. Are you expecting to pass "console" in findControl method or it should be same as "console-popup". Sometimes we do type error and look into different direction.

    Anyway Just to complete the answer you can add this line just after calling find Control method.

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