0

My code used to work perfectly but now when I start the application I get a blank screen and an error.

"Exception in thread "LWJGL Application" java.lang.NullPointerException at com.mygdx.game.Bunny.render(Bunny.java:70) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)"

this indicates to line 70:

batch.draw(paddle, paddleRect.x, paddleRect.y);

My full code is:

package com.mygdx.game;

import java.util.Iterator;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;

public class Bunny extends ApplicationAdapter {
    SpriteBatch batch;
    Texture paddle;
    Texture blockImg;
    Rectangle paddleRect;

    float speed = 300.0f;

    private OrthographicCamera camera;

    private Array<Rectangle> blocks;

    private void spawnBlock(int x, int y){
        Rectangle block = new Rectangle();
        block.x = x*90;
        block.y = 400-(y*30);
        block.width = 30;
        block.height = 10;
        blocks.add(block);
    }

    @Override
    public void create () {
        Rectangle paddleRect = new Rectangle();

        paddleRect.x = 0;
        paddleRect.y = 0;
        paddleRect.width = 60;
        paddleRect.height = 20;

        batch = new SpriteBatch();
        paddle = new Texture("paddle.png");
        blockImg = new Texture("block.png");

        camera = new OrthographicCamera();
        camera.setToOrtho(false, 400, 400);

        blocks = new Array<Rectangle>();

        for(int x = 0; x < 5; x++){
            for(int y = 0; y < 4; y++){
                spawnBlock(x, y);
            }
        }

    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0.5f, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        camera.update();

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.draw(paddle, paddleRect.x, paddleRect.y);
        for(Rectangle block: blocks){
            batch.draw(blockImg, block.x, block.y);
        }
        batch.end();

        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT))
            paddleRect.x += speed * Gdx.graphics.getDeltaTime();
        else if(Gdx.input.isKeyPressed(Input.Keys.LEFT))
            paddleRect.x -= speed * Gdx.graphics.getDeltaTime();
        if(Gdx.input.isKeyPressed(Input.Keys.UP))
            paddleRect.y += speed * Gdx.graphics.getDeltaTime();

        if(paddleRect.x < 0f)
            paddleRect.x = 0f;
        if(paddleRect.x > 340f)
            paddleRect.x = 340f;

        Iterator<Rectangle> iter = blocks.iterator();
        while(iter.hasNext()){
            Rectangle block = iter.next();
            if(block.overlaps(paddleRect))
                iter.remove();
        }
    }
}
  • 2
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – lukelazarovic Sep 15 '15 at 08:30

1 Answers1

3

In create you want to assign a new Rectangle to your class field paddleRect but instead you create a method variable called paddleRect and your class field remains null.

Try:

public void create () {
        paddleRect = new Rectangle();

        paddleRect.x = 0;
        paddleRect.y = 0;
        ...
Sebastian
  • 5,721
  • 3
  • 43
  • 69