Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Animation fails due to a NullPointerException which I'm certain shouldn't be thrown (LibGDX / Java)

$
0
0

What the title says basically. I’ve been stuck on this error for a few hours I got it to work earlier but I honestly can’t see why its failing now (ctrl + z’d back to the working stage). Here’s my code:

public class Animator {
    private Animation animation;
    private TextureRegion[] walkFrames;
    private TextureRegion[] jumpFrames;
    private TextureRegion[] enemyFrames;
    private Texture jumpSheet;
    private Texture walkSheet;
    private Texture enemySheet;
    private TextureRegion currentFrame;
    private Animation enemyAnimation;
    private Animation jumpAnimation;
    private Animation walkAnimation;
    private TextureRegion[][] animationType;
    private TextureRegion[] animateJump;
    private TextureRegion[] animateWalk;
    private TextureRegion[] animateEnemy;
    private GameObject object;
    private float stateTime;

    public Animator(GameObject object) {
        this.object = object;
        jumpSheet = new Texture(Gdx.files.internal("jumpsheet.png"));
        walkSheet = new Texture(Gdx.files.internal("walksheet.png"));
        enemySheet = new Texture(Gdx.files.internal("enemySheet.png"));
        enemyAnimation = animate(1, 4, enemySheet);
        jumpAnimation = animate(1, 7, jumpSheet);
        walkAnimation = animate(1, 4, walkSheet);
        stateTime = 0f;
    }

    public Animation animate(int rows, int cols, Texture spriteSheet) {
        TextureRegion[] frames = new TextureRegion[cols * rows];
        animationType = TextureRegion.split(spriteSheet, spriteSheet.getWidth()
                / cols, spriteSheet.getHeight() / rows);
        int index = 0;
        for (int i = 0; i < frameRows; i++) {
            for (int j = 0; j < frameCols; j++) {
                frames[index++] = animationType[i][j];
            }
        }
        animation = new Animation(0.25f, frames);
        return animation;
    }

    public void render(SpriteBatch batch) {
        stateTime += Gdx.graphics.getDeltaTime();
        if (object.getType() == ObjectType.PLAYER
                && object.getState() == ObjectState.JUMPING) {
            currentFrame = jumpAnimation.getKeyFrame(stateTime, true);
        } else if (object.getType() == ObjectType.PLAYER
                && object.getState() == ObjectState.NORMAL) {
            currentFrame = walkAnimation.getKeyFrame(stateTime, true);
        } else {
            currentFrame = enemyAnimation.getKeyFrame(stateTime, true);
        }
        batch.draw(currentFrame, object.getxPos(), object.getyPos()); // Null Pointer
    }
}

I get the NullPointer on the last line, and if I run in debug mode with breakpoints it tells me that currentFrame is null (at every stage of the if statement). Can anyone see why?

Any help is highly appreciated, thanks.


Viewing all articles
Browse latest Browse all 434

Trending Articles