简体   繁体   English

Slick2D NullPointerException

[英]Slick2D NullPointerException

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: 我在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)

Here is the for the M class (excluding imports and class declaration, they are unneeded): 这是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;
}

The problem is that I.title == null. 问题是I.title == null。 Set it to a correct value. 将其设置为正确的值。

In init function set the url to the image into I.title, i mean => I.title = new Image("-your URL-"); 在init函数中将图像的url设置为I.title,我的意思是=> 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. Slick2D以这种形式工作:首先,他在方法init中初始化所有参数,然后直到发生更改当前状态“he”render => update => render => update => render => update ... You可以使用SetTargetFrameRate方法设置更新将在SetUpClass中发生多少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM