0

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!

1Poseidon3
  • 125
  • 1
  • 11

3 Answers3

4

The variable control is null, and you attempt to call method output() on it. You'll probably want to check it for null in an if statement before attempting to call methods on it. If you're not expecting it to ever be null, try looking at why the previous line is returning null.

final ConsoleControl control = (ConsoleControl)this.nifty.createPopup("console-popup").findControl("console", ConsoleControl.class);
Scott Heaberlin
  • 3,364
  • 1
  • 23
  • 22
  • This may be a bit nooby but why would it return null and how would I check to see if it does? – 1Poseidon3 Mar 01 '14 at 01:07
  • The error stack says `at com.bminus.ttp.controller.HUD.bind(HUD.java:35)`. Check if it is the same line. – Ravinder Reddy Mar 01 '14 at 01:08
  • It is the the line below the one he is specifying. @Ravinder – 1Poseidon3 Mar 01 '14 at 01:10
  • Yes. Line `34` is causing a `null` output for `control` variable. And hence is the NPE at `35`. – Ravinder Reddy Mar 01 '14 at 01:11
  • I don't really know this API you're using. I'm taking you at your word that HUD.java line 35 is the `control.output(...)` line you mention in your post. This means that the `findControl()` method you call is returning `null`. I'd consult the API documentation for that method, but I'd suspect it cannot find any control named "console" of type `ConsoleControl.class` – Scott Heaberlin Mar 01 '14 at 01:12
  • @Ravinder how do I make sure it does **Not** return null anymore though? – 1Poseidon3 Mar 01 '14 at 01:12
  • @1Poseidon3: Make sure that the `findControl` won't return a `null`. – Ravinder Reddy Mar 01 '14 at 01:13
  • @1Poseidon3 one possibility, after looking at nifty-gui source, is definitely that there is no control named "console" in the popup that is created in the result of your `createPopup()` call. https://github.com/void256/nifty-gui/blob/1.4/nifty-core/src/main/java/de/lessvoid/nifty/screen/Screen.java#L433 – Scott Heaberlin Mar 01 '14 at 01:25
1

Check to see if final ConsoleControl control ... returns a null

You could add the statement if (control == null) {System.out.println("I found the problem, now I have to figure out why control is null);}

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
1

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;
}
Mak
  • 596
  • 5
  • 10