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.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.x anywhere else, it returns the correct value. But in the paintcomponent method ive put in System.out.println(row1OfBricks.x); just as a test, and it errors.
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.
Joined: May 21, 2008
omg wow, thanks so much! I feel stupid for not catching that myself.