javanullpointerexceptionslick2d

Slick2D getGraphics() returning null, null pointer exception occurs


I am working on getting my slick2d game to render, but container.getGraphics() is returning null and I am getting a null pointer exception. I checked that it was the getGraphics() that was returning null by using:

if (container.getGraphics()==null) return;

This fixed the npe but then of course I can't render anything.

Stacktrace:

Tue Sep 02 21:42:19 MDT 2014 INFO:Slick Build #237
Exception in thread "Thread-1" java.lang.NullPointerException
    at com.nonuniversal.client.MenuState.render(MenuState.java:17)
    at org.newdawn.slick.state.StateBasedGame.render(StateBasedGame.java:199)
    at com.nonuniversal.client.Game.run(Game.java:33)
    at java.lang.Thread.run(Unknown Source)

please note LunatopiaClient needs a username argument when ran...

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.SlickException;

public class LunatopiaClient{
    private String username;
    AppGameContainer container;
    Game game;

    public LunatopiaClient(String username) {

        this.username = username;

    }
    public static void main(String args[]) {

        if (args.length!=1) {
            System.out.println("Usage: java -jar <filename.jar> <username>");
            return;
        } else {
            LunatopiaClient lunatopiaClient = new LunatopiaClient(args[0]);
            lunatopiaClient.init();
        }

    }
    public void init() {

        try {
            game = new Game("");
            container = new AppGameContainer(game);
            container.setFullscreen(true);
            game.init(container);
            game.init();
        } catch (SlickException e) {
            e.printStackTrace();
        }

    }
    public Game getGame() {
        return game;
    }
    public String getUsername() {
        return username;
    }
}

class Game

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 implements Runnable{
    public static final int menuState = 0;
    public static final int gameState = 1;
    private boolean running;
    private AppGameContainer container;
    Thread game;

    public Game(String name) {
        super(name);
    }
    public void initStatesList(GameContainer container) throws SlickException {
        this.container = (AppGameContainer) container;
        addState(new MenuState());
        addState(new GameState());
    }
    public void init() throws SlickException {
        enterState(menuState);
        game = new Thread(this);
        game.start();
    }
    public void run() {
        running = true;
        while (running) {
            try {
                update(container, 0);
                render(container, container.getGraphics());
            } catch (SlickException e) {
                e.printStackTrace();
            }

        }
    }
}

class MenuState

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 MenuState extends BasicGameState{

    public void init(GameContainer container, StateBasedGame game)
            throws SlickException {

    }
    public void render(GameContainer container, StateBasedGame game, Graphics g)
            throws SlickException {
        g.drawString("Hello World", 0, 0);
    }
    public void update(GameContainer container, StateBasedGame game, int arg)
            throws SlickException {

    }
    public int getID() {
        return 0;
    }

}

edit:spelling


Solution

  • You have unfortunately configured a lot of your code in a very unorthodox manor. For a start Slick2d will automatically thread the execution of your program and therefore you do not need to program a thread yourself.

    You also appear to be calling methods which do not exist, for example init(container) is not defined in game. Additionally, you are storing container as a data value which is not required thus why you are getting a NPE for the graphics context.

    If you are just starting out in Slick2d I suggest checking out thenewboston's video tutorials on how to get started. Otherwise, I believe you may have to restart your project as in its current state it is not usable. Hope this helps :)