0

I am watching TheNewBoston's video on basic Java programing a game. I followed EVERY step. Now, I get this weird error.

EDIT: Now the error is on:

            appgc.start();

The window opens then closes and says:

Exception in thread "main" java.lang.NullPointerException
at javagame.Game.initStatesList(Game.java:19)
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 javagame.Game.main(Game.java:29)

I've have no idea why.

Here is my Game class:

    package javagame;
    import org.newdawn.slick.*;
    import org.newdawn.slick.state.*;

    public class Game extends StateBasedGame {

public static final String gamename = "Untitled ";
public static final int menu = 0;
public static final int play = 1;

public Game(String gamename){
    super(gamename);
    this.addState(new Menu(menu));
    this.addState(new Play(play));
}

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

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

 }

      }

Here is my menu class:

    package javagame;

    import org.lwjgl.input.Mouse;
    import org.newdawn.slick.*;
    import org.newdawn.slick.state.*;

    public class Menu extends BasicGameState {

public Menu(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
    
    
}   

public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
    g.fillOval(75, 100, 100, 100);
    g.drawString("Play Now!", 80, 70 );
    
}

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
    Input input = gc.getInput();
    int xpos = Mouse.getX();
    int ypos = Mouse.getY();
    if((xpos>75 && xpos<175)&& (ypos>160 && ypos<260));{
        if(input.isMouseButtonDown(0)){
            sbg.enterState(1);
        }
}
}


public int getID(){
    return 0;
}
    }

And here is the Play class:

    package javagame;

    import org.lwjgl.input.Mouse;
    import org.newdawn.slick.*;
    import org.newdawn.slick.state.*;

    public class Play extends BasicGameState {

public Play(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 0;
}
    }

Please help me.

General Grievance
  • 4,555
  • 31
  • 31
  • 45

2 Answers2

0

The problem happens at javagame.Game.initStatesList(Game.java:19). I don't know which line that is from your code, but NullPointerException happens when you try to access members of a reference variable that wasn't previously allocated.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
0

You're passing gc to your init method:

this.getState(menu).init(gc, this);
this.getState(play).init(gc, this);

But in your init methods, you don't do anything with them

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

You may want to instantiate them in the method

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720