slick2d nullpointerexception

2g32fytz  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(239)

我正在用slick2d for java制作一个游戏,但是当我试图显示一个图像(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;
}
ryhaxcpt

ryhaxcpt1#

问题是 I.title ==空。将其设置为正确的值。

pcrecxhr

pcrecxhr2#

在init函数中将图片的url设置为i.title,我的意思是=>i.title=new image(“-your url-”);slick2d的工作方式是这样的:首先他是init方法init中的所有参数,然后直到发生了改变当前状态的事情“他”render=>update=>render=>update=>render=>update。。。您可以使用settargetframerate方法设置更新在setupclass中发生的次数。

相关问题