Question

I am making a game in Slick2D for Java, but when I tried to display an Image(the Slick2D type) it gives me this error:

java.lang.NullPointerException
at org.newdawn.slick.Graphics.drawImage(Graphics.java:1384)
at org.newdawn.slick.Graphics.drawImage(Graphics.java:1433)
at luke_r.games.java.broadway.states.M.render(M.java:21)
at org.newdawn.slick.state.StateBasedGame.render(StateBasedGame.java:199)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:688)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
at luke_r.games.java.broadway.C.main(C.java:21)

Here is the for the M class (excluding imports and class declaration, they are unneeded):

@Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
{
    PL.x = 100;
    PL.y = 100;
}
@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
{
    g.drawImage(I.title, 0, 0); //Line 21, the error
    PL.render(g);
}
@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
{}
@Override
public int getID()
{
    return 0;
}
Was it helpful?

Solution

The problem is that I.title == null. Set it to a correct value.

OTHER TIPS

In init function set the url to the image into I.title, i mean => I.title = new Image("-your URL-"); Slick2D works in this form: first of all he is init all params in method init , then until somthing happend that change the current state "he" render => update => render => update =>render => update ... You can set how much the update will occurs in the SetUpClass by using SetTargetFrameRate method.

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