| Author |
Weird error not sure what Im doing wrong
|
Keshan Pillay
Greenhorn
Joined: May 21, 2008
Posts: 29
|
|
Hi everyone! I'm doing an assignment for university (compsci 101), where I have to program a simple 2d game. So I chose brick, (the one with the ball and the paddle) and I've gotten this far. What I'm trying to do here is to make the 3 arrays row1OfBricks, row2OfBricks and row3Ofbricks, then I want to draw the rectangles stored in these arrays in the paintComponent method. The arrays are initialized correctly, and the values they all store are all correct. But when I try an use something like row1OfBricks[1].x in the paintcomponent method, to access the x value of the rectangle in position 1 of the array, I get this mega error that streams down the console and doesn't stop. When I try and access row1OfBricks[1].x anywhere else, it returns the correct value. But in the paintcomponent method ive put in System.out.println(row1OfBricks[1].x); just as a test, and it errors. Any help would be appreciated.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
|
You've got row1OfBricks defined twice - once as an instance variable and once as a local variable in the GUIJPanel constuctor, so the constructor will initialise the local variable, but the paintComponent method will use the instance variable that has not been initialised.
|
Joanne
|
 |
Keshan Pillay
Greenhorn
Joined: May 21, 2008
Posts: 29
|
|
|
omg wow, thanks so much! I feel stupid for not catching that myself.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
|
|
And welcome to JavaRanch. It takes quite a lot of experience to pick up that sort of mistake.
|
 |
Keshan Pillay
Greenhorn
Joined: May 21, 2008
Posts: 29
|
|
|
Thank you very much for the kind welcome, I'm sure I'll be spending a lot of time here ^^
|
 |
 |
|
|
subject: Weird error not sure what Im doing wrong
|
|
|