Search code examples
libgdx

Attempt to read from field 'com.badlogic.gdx.graphics.Texture com.badlogic.gdx.graphics.g2d.TextureRegion.texture' on a null object reference


My game has been published in the play market. The same error comes every day. I can't understand where it comes from. Maybe someone had something similar?

Fatal Exception: java.lang.NullPointerException: Attempt to read from field 'com.badlogic.gdx.graphics.Texture com.badlogic.gdx.graphics.g2d.TextureRegion.texture' on a null object reference
   at com.badlogic.gdx.graphics.g2d.SpriteBatch.draw(SpriteBatch.java:640)
   at durak.cards.game.actors.ButtonsActor.draw(ButtonsActor.java:239)
   at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:111)
   at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:58)
   at com.badlogic.gdx.scenes.scene2d.Group.drawChildren(Group.java:111)
   at com.badlogic.gdx.scenes.scene2d.Group.draw(Group.java:58)
   at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:129)
   at durak.cards.game.screens.GameScreen.render(GameScreen.java:669)
   at com.badlogic.gdx.Game.render(Game.java:46)
   at durak.cards.game.StartGame.render(StartGame.java:151)
   at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:471)
   at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1573)
   at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1272)

I use AssetManager, then unpack the atlas in the ButtonsActor class

public class ButtonsActor extends Actor {

Sprite sprite;
TextureAtlas textureAtlas;
TextureRegion[] textureRegions ;


public ButtonsActor() {
    textureAtlas = new   TextureAtlas ();
    textureAtlas = AssetsManager.button;
    textureRegions = new TextureRegion[41];
    textureRegions[0] = (textureAtlas.findRegion("buttonMinus"));
    textureRegions[1] = (textureAtlas.findRegion("sum"));
    textureRegions[2] = (textureAtlas.findRegion("buttonPlus"));
    textureRegions[3] = (textureAtlas.findRegion("2_users"));
    textureRegions[4] = (textureAtlas.findRegion("3_users"));
    textureRegions[5] = (textureAtlas.findRegion("4_users"));
    textureRegions[6] = (textureAtlas.findRegion("frameMenu"));
    textureRegions[7] = (textureAtlas.findRegion("buttonPlay"));
    textureRegions[8] = (textureAtlas.findRegion("backButton"));
  ///////////////////////////////////////////////////////////////////
    textureRegions[40] = (textureAtlas.findRegion("buttonInfo"));

}

public void spriteChangeB(int tempInt) {
    sprite = new Sprite(textureRegions[tempInt]);
}
@Override
public Actor hit (float x, float y, boolean touchable) {
    if (getTouchable() != Touchable.enabled) return null;
    return x >= 0 && x < this.getWidth() && y >= 0 && y < this.getHeight() ? this : null;
}


@Override
public void draw(Batch batch, float parentAlpha) {

    Color color = getColor();
    batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
    batch.draw(sprite, getX(), getY(), getOriginX(), getOriginY(),
            getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
}

}

I removed all the groups included actors from the ButtonsActor class. Now I use only stage, without groups. But the error still appears.


Solution

  • Are you using only AssetManager.load(...) as this only -starts- loading queuing.

    AssetManager.finishLoading() synchronously completes or you could check with AssetManager.isFinished().

    As some phones have different read performance there may well be a few that don't finish in time.