0
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

assert is your friend, if you get NullPointerExceptions that originates in your code (and not deeper into nested calls) then one of your objects is null, no matter how unlikely or impossible it seems.

You have to assume that all assumptions are wrong and either step through the code and checking variables as you go, or - if that is hard due to the program - add assert(x != null) in your code

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

If you would like more details you will have to post the exceptions stack trace, if those asserts don't catch your issue you are probably misreading the stack trace.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.