我的LibGdx應用程序(Java)中有一個空指針錯誤,我研究了我的所有代碼,但沒有任何內容,主要問題在於java.lang。 NullPointerException異常出現之前batch.draw()
和stage.draw()
方法被調用多次(執行錯誤)不會立即LWJGL應用程序「java.lang.NullPointerException
這裏的錯誤:
: here2
: here3
: here2
: here3
: here2
: here3
: here2
: here3
: here2
: here3
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.graphics.g2d.SpriteBatch.draw(SpriteBatch.java:598)
at com.platanobit.actors.Delivery.draw(Delivery.java:60)
at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:110)
at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:57)
at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:128)
at com.platanobit.screens.GameScreen.render(GameScreen.java:24)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:223)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
這裏的線60的代碼(com.platanobit.actors .Delivery.draw(Delivery.java:6 0):
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
if (dodging) {
batch.draw(dodgingTexture, screenRectangle.x, screenRectangle.y + screenRectangle.height/4, screenRectangle.width,
screenRectangle.height * 3/4);
} else if (hit) {
// When he's hit we also want to apply rotation if the body has been rotated
batch.draw(hitTexture, screenRectangle.x, screenRectangle.y, screenRectangle.width * 0.5f,
screenRectangle.height * 0.5f, screenRectangle.width, screenRectangle.height, 1f, 1f,
(float) Math.toDegrees(body.getAngle()));
} else if (jumping) {
batch.draw(jumpingTexture, screenRectangle.x, screenRectangle.y, screenRectangle.width,
screenRectangle.height);
} else {
// Running
stateTime += Gdx.graphics.getDeltaTime();
if(batch != null){
batch.draw(runningAnimation.getKeyFrame(stateTime, true), screenRectangle.x, screenRectangle.y,
screenRectangle.getWidth(), screenRectangle.getHeight());
} else {
Gdx.app.log("", "here");
}
}
Gdx.app.log("","here2");
}
這裏是從線24(GameScreen.java)代碼:
@Override
public void render(float delta) {
// Clear the screen
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Update the stage
stage.draw();
Gdx.app.log("","here3");
stage.act(delta);
}
注意:請看看調試目的Gdx.app.log("","Here");
代碼。如果有人需要別的東西,請幫助我:/
已經調試過了,以確保你沒有在某處傳遞null。 – Carcigenicate
是的,我做了,我把一些代碼,如if(batcher!= null),顯然沒有null變量,但如果你看到方法batch.draw()和stage.draw()被調用3次以後是空指針:/ –