• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Game of Life Help

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Jerry Van
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jerry Van
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad I could help!
 
Jerry Van
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have put an @ instead of a return type for printCells.
 
Jerry Van
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With @ before "printCells" I get this error:

"printCells cannot be resolved to a type."
 
Jerry Van
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Duh, now I get what you were saying.

Return types of "void", "int", and "int[][]" don't work either.
 
Jerry Van
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'!
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case nobody noticed, most of the assignments are wrong (wrong direction).

Example:

It should be :
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic