Question

I am trying to use java slick to create a basic state based game. The 3 of my stages work fine, but for some reason the other 5 cause a null point exception when I call the get state method. All states are initialized and added to the game in the same manner, and the actual code for Load game (one of the working states) is exactly the same as all the broken ones, minus the class name. I have been looking over this for a day or so and have no idea, any help will be great. Thanks.

This is the code for my game class - containing my main method

package javagame.states;


import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

public class Game extends StateBasedGame{

public static final String gamename = "An OOP Adventure";
public static final int menu = 0;
public static final int play = 1;
public static final int new_game = 2;
public static final int load_game = 3;
public static final int char_select = 4;
public static final int item_found = 5;
public static final int monster_fight = 6;
public static final int inventory = 7;



public Game(String gamename){
  super(gamename);
  this.addState(new Menu(menu));
  this.addState(new Play(play));
  this.addState(new Newgame(new_game));
  this.addState(new Loadgame(load_game));
  this.addState(new Charselect(char_select));
  this.addState(new Itemfound(item_found));
  this.addState(new Monsterfight(monster_fight));
  this.addState(new Inventory(inventory));

}
/*
 */
public void initStatesList(GameContainer gc) throws SlickException{
   this.getState(menu).init(gc, this);
   this.getState(play).init(gc, this);
   this.getState(load_game).init(gc, this);


   //broken states
   this.getState(new_game).init(gc, this);
   this.getState(item_found).init(gc, this);
   this.getState(char_select).init(gc, this);
   this.getState(monster_fight).init(gc, this);
   this.getState(inventory).init(gc, this);
   //end broken states
   this.enterState(menu);
}

public static void main(String[] args) {
   AppGameContainer appgc;
   try{
      appgc = new AppGameContainer(new Game(gamename));
      appgc.setDisplayMode(640, 360, false);
      appgc.start();
   }catch(SlickException e){
      e.printStackTrace();
   }
}

}

This is the code all the broken classes:

 package javagame.states;

 import org.newdawn.slick.GameContainer;
 import org.newdawn.slick.Graphics;
 import org.newdawn.slick.SlickException;
 import org.newdawn.slick.state.BasicGameState;
 import org.newdawn.slick.state.StateBasedGame;

 public class Newgame extends BasicGameState{


    public Newgame(int state){
    }

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

    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{

    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{

    }

    public int getID(){
       return 3;
    }
 }
Was it helpful?

Solution

code for Load game (one of the working states) is exactly the same as all the broken ones, minus the class name

all broken state classes and Loadgame class return getID() == 3. in Game constructor only Loadgame is registered as state with id == 3 - the others are ignored and thus never registered. then in initStatesList() other state classes cannot be found because there were no states registered with ids like 4, 5, 6, 7.

fix all the broken state classes (including Loadgame) as follows:

public class Newgame extends BasicGameState{ private int state; // !!!

public Newgame(int state){
    this.state = state; // !!!
}

...

public int getID(){
   return this.state; // !!!
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top