2
\$\begingroup\$

I am trying to get keyboard input in a separate class so everything doesn't look as messy. I get the following error message

Exception in thread "main" java.lang.NullPointerException
    at com.****.****.Menu.init(Menu.java:33)
    at com.****.****.Main.initStatesList(Main.java:35)
    at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
    at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:393)
    at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:317)
    at com.****.****.Main.main(Main.java:27)

This is what I tried:

Menu Class where I load the methods

public class Menu extends BasicGameState{

KeyboardInput kbi;

public Menu(int state) {

}

@Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

    kbi.init();

}

@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {    
    kbi.render();
}

@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    kbi.update();
}

@Override
public int getID() {

    return 0;
}

} 

KeyboardInput class

public class KeyboardInput {

public KeyboardInput() {

}

public void init() {    

}

public void render() {

}

public void update() {

}
}
\$\endgroup\$
1
\$\begingroup\$

You need to create an instance of your keyboard class.

Change your menu constructor to this:

public Menu(int state) {
    kbi = new KeyboardInput();
}
\$\endgroup\$
  • \$\begingroup\$ Well yup... dunno how I forgot something that obvious x( My bad \$\endgroup\$ – Yuki Nov 8 '15 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.