• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Weird error not sure what Im doing wrong

 
Keshan Pillay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Keshan Pillay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
omg wow, thanks so much! I feel stupid for not catching that myself.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch.

It takes quite a lot of experience to pick up that sort of mistake.
 
Keshan Pillay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the kind welcome, I'm sure I'll be spending a lot of time here ^^
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic