1
\$\begingroup\$

I have an error regarding Null Pointer Exception and I can't find the exact location of the error in my program. I have searched for probable solutions online but I find none of them helpful. I know what Null Pointer Exception is but I can't pinpoint what went wrong in my code. Any help will be appreciated, thanks.

Here's my code:

LevelOneScreen.java

public class LevelOneScreen implements Screen {

    private final ThumbChase app;
    WalkAnimate walkAnimate;
    private Stage stage;
    private Image levelOneImage;
    private Image holdStartImage;
    public Image walkRightImage;
    public Image walkLeftImage;
    public float deltaTime = Gdx.graphics.getDeltaTime();


    public LevelOneScreen(final ThumbChase app){
        this.app = app;
        this.stage = new Stage(new StretchViewport(app.screenWidth,app.screenHeight , app.camera));

    }
    @Override
    public void show() {

        Gdx.input.setInputProcessor(stage);
        walkAnimate = new WalkAnimate();

        levelOneBackground();
        holdStart();
        ninjaWalk();                            ==>ERROR1:LevelOneScreen.java:54


    }



    public void holdStart(){
        Texture holdStartTexture = new Texture("HoldStart.png");
        holdStartImage = new Image(holdStartTexture);
        float holdStartImageW = holdStartImage.getWidth();
        float holdStartImageH = holdStartImage.getHeight();
        float holdStartImgWidth = app.screenWidth*0.8f;
        float holdStartImgHeight = holdStartImgWidth *(holdStartImageH/holdStartImageW);
        holdStartImage.isTouchable();
        holdStartImage.setSize(holdStartImgWidth,holdStartImgHeight);
        holdStartImage.setPosition(stage.getWidth()/2-holdStartImgWidth/2,stage.getHeight()/2-holdStartImgHeight/2);
        stage.addActor(holdStartImage);
        holdStartImage.addListener(new ActorGestureListener(){
            public void touchDown (InputEvent event, float x, float y, int pointer, int button){
                holdStartImage.setVisible(false);
            };
        });


        }


    public void levelOneBackground(){
        Texture levelOneTexture = new Texture("BGBlue Resize.png");
        levelOneImage = new Image(levelOneTexture);
        levelOneImage.setSize(app.screenWidth,app.screenHeight);
        levelOneImage.setPosition(0,0);
        stage.addActor(levelOneImage);
        levelOneImage.addListener(new ActorGestureListener(){
            public void touchDown (InputEvent event, float x, float y, int pointer, int button){
                holdStartImage.setVisible(false);
            };
        });
    }
    public void ninjaWalk(){

ERROR2==>   TextureRegion ninjaWalkRight = new TextureRegion(walkAnimate.getCurrentFrameRight());
            TextureRegion ninjaWalkLeft = new TextureRegion(walkAnimate.getCurrentFrameLeft());
            walkRightImage = new Image(ninjaWalkRight);
            walkLeftImage = new Image(ninjaWalkLeft);
            float walkImageW = walkRightImage.getWidth();
            float walkImageH = walkRightImage.getHeight();
            float walkImageWidth = app.screenWidth*0.15f;
            float walkImageHeight = walkImageWidth*(walkImageH/walkImageW);
            walkLeftImage.isTouchable();
            walkRightImage.isTouchable();
            walkRightImage.setSize(walkImageWidth,walkImageHeight);
            walkLeftImage.setSize(walkImageWidth,walkImageHeight);
            walkRightImage.setPosition(stage.getWidth()/2-walkImageWidth/2,0);
            walkLeftImage.setPosition(stage.getWidth()/2-walkImageWidth/2,0);
            walkLeftImage.addAction(moveBy(3f,3f,3f));
            stage.addActor(walkLeftImage);


        }

        @Override
        public void render(float delta) {

            Gdx.gl.glClearColor(0, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            walkAnimate.update(deltaTime);
            update(delta);
        }

        public void update(float delta){
            stage.act(delta);
            stage.draw();
            app.batch.begin();

            app.batch.end();
        }

        @Override
        public void resize(int width, int height) {

        }

        @Override
        public void pause() {

        }

        @Override
        public void resume() {

        }

        @Override
        public void hide() {

        }

        @Override
        public void dispose() {
            stage.dispose();

        }
    }

WalkAnimate.java

public class WalkAnimate {

        public ThumbChase app;
        public Stage stage;


        private Animation walkAnimationRight;
        private Animation walkAnimationLeft;
        private Texture walkSheetRight;
        private Texture walkSheetLeft;
        private TextureRegion[] walkFramesRight;
        private TextureRegion[] walkFramesLeft;
        private TextureRegion   currentFrameRight;
        private TextureRegion   currentFrameLeft;
        private float stateTime;
        private Rectangle bound; //used for positioning and collision detection
        private static final int  FRAME_COLS_WALK = 3;
        private static final int  FRAME_ROWS_WALK= 2;
        private float screenWidth = Gdx.graphics.getWidth();
        private float screenHeight = Gdx.graphics.getHeight();
        public float currentFrameWidth = (float)(screenHeight*0.15);
        public float currentFrameHeight = (float)(screenHeight*0.15);
        public float walkSheetWidth;
        public float walkSheetHeight;



        public WalkAnimate () {

            walkSheetRight = new Texture("ninjaWalkRight.png");
            walkSheetWidth = walkSheetRight.getWidth();
            walkSheetHeight = walkSheetRight.getWidth();
            TextureRegion[][] tmp = TextureRegion.split(walkSheetRight, (int) walkSheetRight.getWidth() / FRAME_COLS_WALK, (int) walkSheetRight.getHeight() / FRAME_ROWS_WALK);
            walkFramesRight = new TextureRegion[FRAME_COLS_WALK * FRAME_ROWS_WALK];
            int index = 0 ;
            for (int i = 0; i < FRAME_ROWS_WALK; i++) {
                for (int j = 0; j < FRAME_COLS_WALK; j++) {
                    walkFramesRight[index++] = tmp[i][j];
                }
            }
            walkAnimationRight = new Animation(0.044f, walkFramesRight);
            stateTime = 0f;


            walkSheetLeft = new Texture("ninjaWalkLeft.png");
            walkSheetWidth = walkSheetLeft.getWidth();
            walkSheetHeight = walkSheetLeft.getWidth();
            TextureRegion[][] tmp1 = TextureRegion.split(walkSheetLeft, (int) walkSheetRight.getWidth() / FRAME_COLS_WALK, (int)walkSheetLeft.getHeight() / FRAME_ROWS_WALK);
            walkFramesLeft = new TextureRegion[FRAME_COLS_WALK * FRAME_ROWS_WALK];
            int index1 = 0;
            for (int i = 0; i < FRAME_ROWS_WALK; i++) {
                for (int j = 0; j < FRAME_COLS_WALK; j++) {
                    walkFramesLeft[index1++] = tmp1 [i][j];
                }
            }
            walkAnimationLeft = new Animation(0.044f, walkFramesLeft);
            stateTime = 0f;


        }


        public Rectangle getBound(){
            return bound;
        }

        public void update(float delta){
            stateTime += delta;
            currentFrameRight = walkAnimationRight.getKeyFrame(stateTime, true);
            currentFrameLeft = walkAnimationLeft.getKeyFrame(stateTime, true);
        }


        public TextureRegion getCurrentFrameRight(){
            return currentFrameRight;

        }
        public TextureRegion getCurrentFrameLeft(){
            return currentFrameLeft;
        }
    }

And here's the error:

> java.lang.NullPointerException
>                                                                                   at
> com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion(TextureRegion.java:112)
>                                                                                   at
> com.badlogic.gdx.graphics.g2d.TextureRegion.<init>(TextureRegion.java:63)
>                                                                                   at
> com.jpfalmazan.thumbchaseninja.GameScreens.LevelOneScreen.show(LevelOneScreen.java:54)
>                                                                                   at com.badlogic.gdx.Game.setScreen(Game.java:61)
>                                                                                   at
> com.jpfalmazan.thumbchaseninja.GameScreens.MenuScreen$1.clicked(MenuScreen.java:82)
>                                                                                   at
> com.badlogic.gdx.scenes.scene2d.utils.ClickListener.touchUp(ClickListener.java:89)
>                                                                                   at
> com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:58)
>                                                                                   at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java:353)
>                                                                                   at
> com.badlogic.gdx.backends.android.AndroidInput.processEvents(AndroidInput.java:379)
>                                                                                   at
> com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:457)
>                                                                                   at
> android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
>                                                                                   at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
\$\endgroup\$
3
\$\begingroup\$

You don't initialize your currentframeRight and currentFrameLeft in the constructor.

Put this in the constructor of WalkAnimate:

currentFrameRight = walkAnimationRight.getKeyFrame(0f, true);
currentFrameLeft = walkAnimationLeft.getKeyFrame(0f, true);

So it looks like this:

public WalkAnimate () {

        walkSheetRight = new Texture("ninjaWalkRight.png");
        walkSheetWidth = walkSheetRight.getWidth();
        walkSheetHeight = walkSheetRight.getWidth();
        TextureRegion[][] tmp = TextureRegion.split(walkSheetRight, (int) walkSheetRight.getWidth() / FRAME_COLS_WALK, (int) walkSheetRight.getHeight() / FRAME_ROWS_WALK);
        walkFramesRight = new TextureRegion[FRAME_COLS_WALK * FRAME_ROWS_WALK];
        int index = 0 ;
        for (int i = 0; i < FRAME_ROWS_WALK; i++) {
            for (int j = 0; j < FRAME_COLS_WALK; j++) {
                walkFramesRight[index++] = tmp[i][j];
            }
        }
        walkAnimationRight = new Animation(0.044f, walkFramesRight);
        stateTime = 0f;


        walkSheetLeft = new Texture("ninjaWalkLeft.png");
        walkSheetWidth = walkSheetLeft.getWidth();
        walkSheetHeight = walkSheetLeft.getWidth();
        TextureRegion[][] tmp1 = TextureRegion.split(walkSheetLeft, (int) walkSheetRight.getWidth() / FRAME_COLS_WALK, (int)walkSheetLeft.getHeight() / FRAME_ROWS_WALK);
        walkFramesLeft = new TextureRegion[FRAME_COLS_WALK * FRAME_ROWS_WALK];
        int index1 = 0;
        for (int i = 0; i < FRAME_ROWS_WALK; i++) {
            for (int j = 0; j < FRAME_COLS_WALK; j++) {
                walkFramesLeft[index1++] = tmp1 [i][j];
            }
        }
        walkAnimationLeft = new Animation(0.044f, walkFramesLeft);
        stateTime = 0f;


        // add this !
        currentFrameRight = walkAnimationRight.getKeyFrame(0f, true);
        currentFrameLeft = walkAnimationLeft.getKeyFrame(0f, true);


    }

Why?

In your show() method you call ninjaWalk(), and ninjaWalk() calls this: walkAnimate.getCurrentFrameRight() but walkAnimate didn't update yet so it doesn't have assigned any texture in currentFrameLeft or Right.

And here is why it isn't animating

@Override
public void render(float delta) {

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    walkAnimate.update(deltaTime); // you have two deltas, use delta
    update(delta);
}

You have two deltas in your class. Remove the whole deltaTime variable and use only delta from render() like this walkAnimate.update(delta);

\$\endgroup\$
  • \$\begingroup\$ I have already done that on my update method, or am I placing it wrong? \$\endgroup\$ – JAlmazan Nov 23 '16 at 8:46
  • \$\begingroup\$ Wow it finally works!! The animation isn't animating though, it only shows one frame. \$\endgroup\$ – JAlmazan Nov 23 '16 at 8:50
  • \$\begingroup\$ See the answer why there was a null. I'm reading the code to find out why it's not animating, just a sec \$\endgroup\$ – Jacob Nov 23 '16 at 8:59
  • \$\begingroup\$ Check the answer! :) \$\endgroup\$ – Jacob Nov 23 '16 at 9:03
  • \$\begingroup\$ Can you take a look at my question here: gamedev.stackexchange.com/questions/136318/… \$\endgroup\$ – JAlmazan Jan 25 '17 at 8:24

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.