aspose file tools*
The moose likes Beginning Java and the fly likes Game of Life Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Game of Life Help" Watch "Game of Life Help" New topic
Author

Game of Life Help

Jerry Van
Greenhorn

Joined: May 03, 2008
Posts: 20
Yes, this is a school project. I'm working on it in Eclipse. When I try to run Debug on the LifeGameTester class, it terminates on the "game.setBoard(board);" line, and says that the cells field is null. I can't figure out why.

Here's the LifeGameTerter class:



Please tell me if you need to see any of the other classes in the project. And, I'm sorry if this is a worn out topic, I couldn't find any related threads doing a search.

Thanks!,
Van
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

As of this program, it doesn't seems to be possible ( ) for me to identify the exact problem, may be you may need to post other code too...


The Best way to predict your future is to create it - Every great individual common man
Jerry Van
Greenhorn

Joined: May 03, 2008
Posts: 20
What's the easiest way to do that? There's 13 classes in the project.

Why didn't the code tags work? There was one on both ends, like this:



There shold be a Preview Post option.
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Originally posted by Jerry Van:
When I try to run Debug on the LifeGameTester class, it terminates on the "game.setBoard(board);" line, and says that the cells field is null. I can't figure out why.


Sounds like you need to figure out why


is returning a board with null cells, or why game.setBoard(board) thinks cells is null.


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Jerry Van
Greenhorn

Joined: May 03, 2008
Posts: 20
Originally posted by Stevi Deter:


Sounds like you need to figure out why


is returning a board with null cells, or why game.setBoard(board) thinks cells is null.



Yes, that is my problem. How do I figure that out? All that Eclipse debugger gives me is a NullPointerException error on the "game.setBoard(board)" line.

Here's the Game class, which houses the setBoard method:

Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Jerry,

Have you tried setting breakpoints and stepping through the code? That will help you check each value.

If you're getting a NPE, why do you think it's because the cells field is null? On that specific line, the only way I can see that code throwing an NPE is if game is null (cannot call a method on a null object).

Are you sure game is initialized? Your implementation of getInstance() is highly suspect:


Try stepping through the code and verifying everything is being set as you expect.
[ May 13, 2008: Message edited by: Stevi Deter ]
Jerry Van
Greenhorn

Joined: May 03, 2008
Posts: 20
Originally posted by Stevi Deter:
Jerry,

If you're getting a NPE, why do you think it's because the cells field is null? On that specific line, the only way I can see that code throwing an NPE is if game is null (cannot call a method on a null object).


[ May 13, 2008: Message edited by: Stevi Deter ]


I have a BreakPoint set on the "game.setBoard(board);" line. When Debug stops on that line, the Variables View shows cells as having a value of null. See Debug1.doc, here:

Debug1

Since I am inexperienced with both Java and Eclipse, I was just assuming this was why Debug was stopping. When I step through the breakpoint, that's when I get the NPE on the "game.setBoard(board);" line.

See Debug2.doc at the above link.

What else should I be looking at?
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

When the debugger breaks, check Variables window for game (or hover your cursor over the line of code). Is it null?

Set the breakpoint at the line where you're getting the instance of game and step into the code and see what's happening.
Jerry Van
Greenhorn

Joined: May 03, 2008
Posts: 20
Yes, when the debugger breaks, game shows a value of null in the Variables view. I thought that's what it was supposed to be, since the instructions say:

"Add a public static method getInstance() to the Game class that will return instance of the class stored in theInstance field . In the method check first if field�s value is null, and if it is create new Game instance and assign it to the field."

I'm probably just not implementing that method correctly.

When I step into the code "game.setBoard(board);", I get what you see in:

Debug2
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Jerry,

I think you're misreading:
In the method check first if field�s value is null, and if it is create new Game instance and assign it to the field.


To be more explict, perhaps it should say
"In the method check first if the instance field's value is null. If it is null, create a new Game and assign it to the field. Return the field."

The point of a Singleton pattern object (which is what you're doing here) is to be sure there's one and only one of the object. Game.getInstance() should always return that one and only one object. As specified, you're just delaying creation of the instance until the first time one is needed.

As written, your code can never create a game; game will always be null. So you can never play. The call to setBoard(board) will always return a NPE because you can never call call a method on a null object.
Jerry Van
Greenhorn

Joined: May 03, 2008
Posts: 20
Originally posted by Stevi Deter:
Jerry,

To be more explict, perhaps it should say
"In the method check first if the instance field's value is null. If it is null, create a new Game and assign it to the field. Return the field."




Okay, if I put:



under the "if" statement. Then I get an error from Eclipse saying that "Cannot instantiate the type Game". That's probably because the class is abstract. Well, again, that's what the instructions said to make the class. So, I am cornfused.
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Jerry,

You're right, you can't instantiate an abstract class.

Given that you're making this call to get your instance:



where do you think the getInstance() call should be implemented?

Not having seen the instructions, I can't give much more guidance without fear of sending you down a path that strays from the requirements.
Jerry Van
Greenhorn

Joined: May 03, 2008
Posts: 20
Originally posted by Stevi Deter:
Jerry,

You're right, you can't instantiate an abstract class.

Given that you're making this call to get your instance:



where do you think the getInstance() call should be implemented?




Aha, it should be in the TwoDimensionalGame class!
Jerry Van
Greenhorn

Joined: May 03, 2008
Posts: 20
Moved getInstance method to the TwoDimensionalBoard class, fixed a couple of things, and Bingo!

At least now I'm past that roadblock. I'll post later what happens next, stay tuned.

Thanks a million Stevi!

(Jerry's wagging his tail now, he's feeling like one of his green horns is beginning to grow.)

Here's the new getInstance method:


[ May 13, 2008: Message edited by: Jerry Van ]
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Glad I could help!
Jerry Van
Greenhorn

Joined: May 03, 2008
Posts: 20
One more problem...

I have this LifeGameTester class:



All I want it to do is print out to the console the contents of the array "cells" when I run LifeGameTester class. with this code Eclipse says printCells cannot be resolved to a type. what does that mean and how do I fix it?

Thanks,
Jerry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36513
    
  16
You have put an @ instead of a return type for printCells.
Jerry Van
Greenhorn

Joined: May 03, 2008
Posts: 20
With @ before "printCells" I get this error:

"printCells cannot be resolved to a type."
Jerry Van
Greenhorn

Joined: May 03, 2008
Posts: 20
Duh, now I get what you were saying.

Return types of "void", "int", and "int[][]" don't work either.
Jerry Van
Greenhorn

Joined: May 03, 2008
Posts: 20
Well, I figured out how to get a printout of the cells, but it'snot exactly what I wanted.

With this code:



I get one long vertical column of the array content. Not exactly what I wanted, but at least it's something. Now, if I can just figure out how to make it look like the array, I'd be shootin' tootin'!
David Balažic
Ranch Hand

Joined: May 15, 2008
Posts: 86
In case nobody noticed, most of the assignments are wrong (wrong direction).

Example:

It should be :
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Originally posted by Jerry Van:
Well, I figured out how to get a printout of the cells, but it'snot exactly what I wanted.

With this code:



I get one long vertical column of the array content. Not exactly what I wanted, but at least it's something. Now, if I can just figure out how to make it look like the array, I'd be shootin' tootin'!


System.out.println(cells[i][j]) prints the value of cells[i][j] followed by a new line. Try System.out.print(cells[i][j]) instead. You will now get all the values on a single line, so you will need to add a newline every so often. System.out.println() will do this for you - you just need to work out where to put it.


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Game of Life Help
 
Similar Threads
Trouble with symbols not being found.
Game Of Life, some questions
printing character array vertically with loop
2D array battleship
Game Tutorials -->> TicTacToe