2016-10-03 62 views
0

我的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");代碼。如果有人需要別的東西,請幫助我:/

+0

已經調試過了,以確保你沒有在某處傳遞null。 – Carcigenicate

+0

是的,我做了,我把一些代碼,如if(batcher!= null),顯然沒有null變量,但如果你看到方法batch.draw()和stage.draw()被調用3次以後是空指針:/ –

回答

0

仔細檢查以確保SpriteBatch在傳入此方法之前已初始化,並確保已調用SpriteBatch.begin();如果這不是問題,請指定到底是什麼行Delivery.java:60

+0

這是第60行:batch.draw(runningAnimation.getKeyFrame(stateTime,true),screenRectangle.x,screenRectangle.y, –

+0

放入檢查以確保它不返回null runningAnimation.getKeyFrame stateTime,true) –

+0

看看我發佈的代碼,是否檢查if(batch!= null){ –

0

在繪製屏幕上的任何東西之前,您應該啓動spriteBatch,繪製完成後應該結束Sprite batch.A Batch用於繪製2D引用紋理(區域)的矩形。該類將批處理繪圖命令並優化它們以供GPU處理。 要用批處理繪製東西,必須先調用begin()方法,該方法將設置適當的渲染狀態。當你完成繪圖時,你必須調用end()來實際繪製你指定的東西。

public void render(SpriteBatch batch){ 
batch.begin(); 
stage.draw(); 
sprite.draw(batch); 
batch.end();