Have a bit of basic code put into the beginning of a game and have run into a bit of a hang with this nasty NullPointerException for no apparent reason.
Loader.java (main class)
public class Loader extends StateBasedGame {
public static final int menu = 0;
public static final int ingame = 1;
public Loader() {
super("insertGameName");
this.addState(new Menu(menu));
this.addState(new Ingame(ingame));
}
public static void main(String args[]) throws SlickException {
AppGameContainer gameContainer = new AppGameContainer(new Loader());
gameContainer.setDisplayMode(rpg.Settings.WIDTH, rpg.Settings.HEIGHT, false);
gameContainer.setVSync(true);
try {
gameContainer.start();
} catch (SlickException e) {
e.printStackTrace();
}
}
public void initStatesList(GameContainer arg0) throws SlickException {
this.getState(0).init(arg0, this);
// this.getState(1).init(arg0, this);
this.enterState(menu);
}
}
and Menu.java (Initial game state)
public class Menu extends BasicGameState {
Image logo;
public Menu(int menu) {
}
public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
logo = new Image("res/logo.png");
}
public void render(GameContainer arg0, StateBasedGame arg1, Graphics g) throws SlickException {
g.setBackground(Color.red);
g.drawImage(logo, 0, 0);
}
public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException {
}
public int getID() {
return 0;
}
}
The NullPointerException is at Menu.java:23 (g.drawImage(logo, 0, 0); I was under the impression that all of this code had been done correctly, so it surprised me when I found out otherwise.
Side note: When fiddling with the code a bit, I found that changing 'logo' in g.drawImage(logo,0,0) for 'newImage("res/logo.png")' stopped the NullPointerException, but doesn't show the image, only the blank red background.