This site requires JavaScript, please enable it in your browser!
Greenfoot back
camsterb
camsterb wrote ...

2012/12/10

Returning the status of an object

camsterb camsterb

2012/12/10

#
Hi. Doing one of those "game of life" thingies. Long story short, I have a grid of about 2500 squares. Each square is an object in an array. Each square is randomly assigned a true or false boolean status (to signify alive and dead). What I need is just to understand how to return the boolean status of one of these square objects at a specific location. The line labelled with "******" returns a java.lang.NullPointerException error. So I call a method to check the boolean status of an object, but apparently it doesn't quiet work. I'll post more code if needed. I'm still pretty new to this, so it's pretty hard for me to see what wrong. While I'm here, can someone please explain why " Cell temp = (Cell) getOneObjectAtOffset(-1,0,Cell.class);" always needs the "(Cell)" part. I haven't a clue what the means or why I need it for some methods in greenfoot. Anyway, the problematic code: public boolean getStatus(){ return alive; } private void countCells() { Cell temp = (Cell) getOneObjectAtOffset(-1,0,Cell.class); ****** if(temp.getStatus() == true) ********* { setStatus(false); System.out.println("cats"); } Thanks in advance
danpost danpost

2012/12/10

#
You grid has edge squares, and if the square in question is an edge square and you check an offset off the grid, you will get that error. You need to check the coordinates to see if they refer to a location where a grid square resides before returning the cell at that offset.
camsterb camsterb

2012/12/10

#
OK. I'll try figure out a way to implement that. I tried that already with an if statement to check if the getX() or getY() is outside of the height or width of the grid, but I got the same error. Is the error is consistent with that mistake of going outside the grid? I'm sure I'll figure it out.
camsterb camsterb

2012/12/10

#
I think I got it. Hopefully I'll solve the rest of my project. Thanks for the great help.
You need to login to post a reply.