getKeyFrame(float, boolean)' on a null object reference

Anything libgdx related goes here!

getKeyFrame(float, boolean)' on a null object reference

Postby TruePlayer » Tue Aug 20, 2019 9:48 pm

I already worked in this style and my player class works fine.
when adding an actor generating enemies to the scene, I get an exception on EnGenScene.draw(EnGenScene.java:71):
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object com.badlogic.gdx.graphics.g2d.Animation.getKeyFrame(float, boolean)' on a null object reference


Code: Select all
public class EnGenScene extends Actor {

    private Array<EnemyScene> enemies;


    private Texture enemyHumanRun;
    private Texture enemyHumanAttack;
    private Texture enemyDogRun;
    private Texture enemyDogAttack;

    private Animation humanRunAnimation;
    private Animation humanAttackAnimation;
    private Animation dogRunAnimation;
    private Animation dogAttackAnimation;

    private Array<TextureRegion> frames;
    private TextureRegion currFrame;
    private TextureRegion[][] tmp;

    private float min = 0.7f;
    private float max = 2.0f;
    private Random random;
    private float timeLeftNewEnemy;

    private static final float RUNNING_FRAME_DURATION = 0.03f;
    private static final float JUMPING_FRAME_DURATION = 0.1f;

    public EnGenScene(){
        enemies = new Array<EnemyScene>();
        random = new Random(System.nanoTime());
        timeLeftNewEnemy = min + random.nextFloat() * (max-min);
    }

    @Override
    public void act(float delta) {
        for (EnemyScene enemy : enemies) {
            if(enemy.getPosition().x <= 2.5f) {
                enemy.setCurrentState(EnemyScene.State.ATTACK);
            }
            if(enemy.getCurrentType().equals(EnemyScene.Type.DOG) && enemy.getPosition().x <= 2f){
                enemy.getPosition().y = 3.55f;
            }
            enemy.update(delta);
        }
        timeLeftNewEnemy -= delta;
        if (timeLeftNewEnemy <= 0) {
            spawnEnemy(random.nextBoolean());
            timeLeftNewEnemy = min + random.nextFloat() * (max-min);
        }
    }

    @Override
    public void draw(Batch batch, float parentAlpha){
        for(EnemyScene enemy : enemies) {
            if (enemy.getCurrentType().equals(EnemyScene.Type.HUMAN)) {
                if (enemy.getCurrentState().equals(EnemyScene.State.RUNNING)) {
                    currFrame = (TextureRegion) humanRunAnimation.getKeyFrame(enemy.getStateTime(), true);
                } else if (enemy.getCurrentState().equals(EnemyScene.State.ATTACK)) {
                    currFrame = (TextureRegion) humanAttackAnimation.getKeyFrame(enemy.getStateTime(), true);
                }
            } else if (enemy.getCurrentType().equals(EnemyScene.Type.DOG)) {
                if (enemy.getCurrentState().equals(EnemyScene.State.RUNNING)) {
                    currFrame = (TextureRegion) dogRunAnimation.getKeyFrame(enemy.getStateTime(), true);
                } else if (enemy.getCurrentState().equals(EnemyScene.State.ATTACK)) {
                    currFrame = (TextureRegion) dogAttackAnimation.getKeyFrame(enemy.getStateTime(), true);
                }
            }

            batch.draw(currFrame,
                    enemy.getPosition().x,
                    enemy.getPosition().y,
                    enemy.getCurrentType().equals(EnemyScene.Type.DOG) ? enemy.getSIZE_X() : enemy.getSIZE_X() * 1.5f,
                    enemy.getCurrentType().equals(EnemyScene.Type.DOG) ? enemy.getSIZE_Y() * 1.5f : enemy.getSIZE_Y() * 2);
        }
    }

    private void init(){
        frames = new Array<TextureRegion>();


        enemyHumanRun = new Texture(Gdx.files.internal("animation/enemy/human/run.png"));
        enemyHumanAttack = new Texture(Gdx.files.internal("animation/enemy/human/attack.png"));
        enemyDogRun = new Texture(Gdx.files.internal("animation/enemy/dog/run.png"));
        enemyDogAttack = new Texture(Gdx.files.internal("animation/enemy/dog/attack.png"));

        /*****************HUMAN RUN ANIMATION************************/
        tmp = TextureRegion.split(enemyHumanRun,
                enemyHumanRun.getWidth() / 8,
                enemyHumanRun.getHeight() / 1);
        for (int i = 0; i < 1; i++)
            for (int j = 0; j < 8; j++)
                frames.add(tmp[i][j]);
        humanRunAnimation = new Animation<TextureRegion>(RUNNING_FRAME_DURATION, frames);
        frames.clear();

        /*****************HUMAN ATTACK ANIMATION************************/
        tmp = TextureRegion.split(enemyHumanAttack,
                enemyHumanAttack.getWidth() / 8,
                enemyHumanAttack.getHeight() / 1);
        for (int i = 0; i < 1; i++)
            for (int j = 0; j < 8; j++)
                frames.add(tmp[i][j]);
        humanAttackAnimation = new Animation<TextureRegion>(JUMPING_FRAME_DURATION, frames);
        frames.clear();

        /*****************DOG RUN ANIMATION************************/
        tmp = TextureRegion.split(enemyDogRun,
                enemyDogRun.getWidth() / 1,
                enemyDogRun.getHeight() / 7);
        for (int i = 0; i < 7; i++)
            for (int j = 0; j < 1; j++)
                frames.add(tmp[i][j]);
        dogRunAnimation = new Animation<TextureRegion>(JUMPING_FRAME_DURATION, frames);
        frames.clear();

        /*****************DOG ATTACK ANIMATION************************/
        tmp = TextureRegion.split(enemyDogAttack,
                enemyDogAttack.getWidth() / 1,
                enemyDogAttack.getHeight() / 7);
        for (int i = 0; i < 7; i++)
            for (int j = 0; j < 1; j++)
                frames.add(tmp[i][j]);
        dogAttackAnimation = new Animation<TextureRegion>(0.3f, frames);
        frames.clear();
    }

    public void spawnEnemy(boolean type){
        EnemyScene enemy = new EnemyScene(new Vector2(11, 1.1f), type);
        enemies.add(enemy);
    }
}




Code: Select all
public class EnemyScene {

    public enum State {RUNNING, ATTACK}
    private State currentState;

    public enum Type {HUMAN, DOG}
    private Type currentType;

    private float SIZE_X;
    private float SIZE_Y;
    private float speed;
    private Vector2 position;
    private Rectangle bounds;
    private float stateTime;

    public EnemyScene(Vector2 pos, boolean type){
        this.position = pos;
        this.bounds = new Rectangle();
        if(type){
            this.SIZE_X = 1f;
            this.SIZE_Y = 1f;
            currentType = Type.HUMAN;
            this.bounds.width = SIZE_X - (SIZE_X / 4 * 2);
            this.bounds.height = SIZE_Y - (SIZE_Y / 5 * 2);
        }else{
            this.SIZE_X = 1f;
            this.SIZE_Y = 0.7f;
            currentType = Type.DOG;
            this.bounds.width = SIZE_X - (SIZE_X / 5 * 2);
            this.bounds.height = SIZE_Y - (SIZE_Y / 5 * 2);
        }
        this.currentState = State.RUNNING;
        this.stateTime = 0f;
        this.speed = 0.09f;
    }

    public void update(float delta) {
        stateTime += delta;
        this.position.x -= speed;
    }

    public Rectangle getBounds() {
        return this.bounds;
    }

    public Vector2 getPosition() {
        return this.position;
    }

    public float getStateTime() {
        return this.stateTime;
    }

    public State getCurrentState() {
        return currentState;
    }
    public void setCurrentState(State currentState) {
        this.currentState = currentState;
    }

    public Type getCurrentType() {
        return currentType;
    }

    public float getSIZE_X() {
        return this.SIZE_X;
    }

    public float getSIZE_Y(){
        return this.SIZE_Y;
    }
}



for a long time I can not understand why this is happening
TruePlayer
 
Posts: 103
Joined: Sun Jul 28, 2019 12:22 pm

Re: getKeyFrame(float, boolean)' on a null object reference

Postby dsgraham » Tue Aug 20, 2019 10:09 pm

Looks like you need to call init() in your EnGenScene constructor? Or is that called from somewhere else? That would be why the Array is empty.
Try out my game PixArt on Android Find it in the Gallery
dsgraham
 
Posts: 86
Joined: Mon Dec 22, 2014 9:28 pm

Re: getKeyFrame(float, boolean)' on a null object reference

Postby TruePlayer » Tue Aug 20, 2019 10:11 pm

god damn))))))))))))))))0000000
TruePlayer
 
Posts: 103
Joined: Sun Jul 28, 2019 12:22 pm


Return to Libgdx

Who is online

Users browsing this forum: No registered users and 1 guest