我正在使用Slick2D for Java制作游戏,但当我尝试显示一个Image(Slick2D类型)时,它给了我这个错误:
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)
这是M类(不包括导入和类声明,不需要它们):
@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;
}
答案 0 :(得分:2)
问题是I.title
== null。将其设置为正确的值。
答案 1 :(得分:0)
在init函数中将图像的url设置为I.title,我的意思是=> I.title =新图片(" -your URL - "); Slick2D以这种形式工作:首先,他在方法init中初始化所有参数,然后直到发生改变当前状态的所有参数#34;他" render => update => render => update => render =>更新...您可以使用SetTargetFrameRate方法设置SetUpClass中的更新量。