0

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!

  • 1
    It seems to me that `front` is `null`. Did `front` get properly initialized in the `init()` method? That is, was it able to find `res/steveFront.png` and was that correctly processed into an instance of `Image`? – Peter Gluck Nov 09 '12 at 00:46
  • Player.front = new Image("res/steveFront.png"); – case1352 Nov 09 '12 at 00:48
  • Yes, everything is coded correctly, however it just doesn't want to work. I have drawn a map using an array the same way. I have no clue as to why it just won't work. –  Nov 09 '12 at 00:51
  • @case1352 I put that in the game class? –  Nov 09 '12 at 00:52
  • im just curious as to why 'front' is static. – case1352 Nov 09 '12 at 00:52
  • @case1532 I can't believe I haven't thought of that... It worked, post it as an answer! –  Nov 09 '12 at 00:53

2 Answers2

1

Usually this means the path you give for the image is wrong

 front = new Image("res/steveFront.png")

If this is in your src folder, you have to use getResource

thedayofcondor
  • 3,860
  • 1
  • 19
  • 28
  • I'm using slick2d which has a resource manager built in, no need for any getResources methods. Thanks though! The problem has been solved. –  Nov 09 '12 at 00:54
0

Try making variable 'front' not static

case1352
  • 1,126
  • 1
  • 13
  • 22