NullPointerException and best way to to debug it..

Hey guys…Having more issues on my brickbreaker game. I’m drawing the bricks in but getting a null pointer exception…I’ve just been toying around with it trying to find the issue. I know there has to be a better way to do this.


package com.psillicoder.brickbreaker;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
//import com.badlogic.gdx.graphics.Texture.TextureFilter;
//import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import java.util.ArrayList;

public class Game implements ApplicationListener {
	private OrthographicCamera camera;
	private SpriteBatch batch;

	private Ball ball;
	private Paddle paddle;
	
	Brick[] bricks;

	//private TextureRegion region;
	
	//rectangles
	
	
	
	@Override
	public void create() {		
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
	
		ball = new Ball();
		paddle = new Paddle();
		bricks = new Brick[10];
		loadBricks();
		
		
		
		//region = new TextureRegion(paddleImage,0,0,64,16);
		
		camera = new OrthographicCamera(1, h/w);
		batch = new SpriteBatch();
		
		
		
		
		
		}
		

	@Override
	public void dispose() {
		batch.dispose();
		//ballImage.dispose();
		
	}

	@Override
	public void render() {		
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		//camera.update();

		
		
		paddle.CheckKeys();
		//Ball.CheckCollision(Paddle.paddleRect);
		ball.checkPaddleCollision(paddle);
		ball.moveBall();
		
		//batch.setProjectionMatrix(camera.combined);
		batch.begin();
		
		for (int i = 0; i <bricks.length;i++)
		{
			bricks[i].draw(batch);
		}
		
		ball.draw(batch);
		paddle.draw(batch);
		batch.end();
	}

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

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	
	public void loadBricks() {
		int i = 0;
		
		for(int j= 0; j < 10;j++) {
			for (int k = 0;k < 10; k++) {
			
			bricks[i] = new Brick(0,j*32,k*16);
			
			}
		
		}
		i++;
	}
	
	
	
	}



I’m getting:


Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.NullPointerException
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:113)
Caused by: java.lang.NullPointerException
	at com.psillicoder.brickbreaker.Game.render(Game.java:80)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:187)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)

Just do System.out.println() on a bunch of objects. When one of them prints “null” then you know that it’s causing the problem.

Based on the line number 80 you know what is causing the error (bricks), so why not just

System.out.println(bricks[i] + " " + i)

before the drawing method of each one (line 79 in this case, but not replacing line 79)? Then you know which brick is causing it, and then you can find out how why it’s causing it.

loadBricks() stores all bricks into index 0

Nah, better to use a debugger. Highlight + ctrl shift i is faster than typing in system.out.println(variable)

I figured it out. I was calling too many bricks that weren’t set up. Thanks for the new ways to debug.

If you are using Eclipse, then Exception Breakpoint is another quick way to locate issues and has helped me a lot.
Just set the debugger to trigger on NullpointerException and it will stop your program on the line where the Nullpointer occurs.

http://www.cavdar.net/2008/09/13/5-tips-for-debugging-java-code-in-eclipse/