Search code examples
javaslick2d

JavaNullPointer for image


I have this code for my player class:

public class Player {

private static Image front;

int posX = 400;
int posY = 300;

public void player() throws SlickException{
    init(null, null);
    render(null, null, null);
    update(null, null, (Integer) null);

}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
    front = new Image("res/steveFront.png");
}

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
    front.draw(posX, posY);
}

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

}   

}

And this is my main game class:

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
    map.render(gc, sbg, g);
    player.render(gc, sbg, g);
}

When I run the code it calls a javanullpointer exception on the

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
    front.draw(posX, posY);
}

Why does it do this? I've been trying to figure this out for hours now. Any help would be appreciated!


Solution

  • Try making variable 'front' not static