Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  NullPointerException and best way to to debug it..  (Read 877 times)
0 Members and 1 Guest are viewing this topic.
Offline Bassex96

Senior Member


Medals: 4



« Posted 2013-03-31 17:32:45 »



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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
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:

1  
2  
3  
4  
5  
6  
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)
Offline tyeeeee1
« Reply #1 - Posted 2013-03-31 17:37:29 »

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.
Offline Sammidysam
« Reply #2 - Posted 2013-03-31 18:00:04 »

Based on the line number 80 you know what is causing the error (bricks), so why not just
1  
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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #3 - Posted 2013-03-31 18:02:41 »

loadBricks() stores all bricks into index 0

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2013-03-31 18:05:23 »

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

Senior Member


Medals: 4



« Reply #5 - Posted 2013-03-31 18:13:35 »

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

Innocent Bystander





« Reply #6 - Posted 2013-04-01 13:52:39 »

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/
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (15 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!