• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with SimpleDotComGame from the book Head First Java.  RSS feed

 
Craig E. Lewis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well as the title says I'm having a difficulty with a simple battleship on a 1D line of cells from 0 - 6. As the book say I have coded three classes the SimpleDotComGame class, the SimpleDotCom class and the GameHelper class. They as follows:




The different classes compiled fine, the problem occurred when I tried to bomb a cell it gave me this error message:
java.lang.NullPointerException
at SimpleDotCom.checkYourself(SimpleDotCom.java:13)
at SimpleDotComGame.main(SimpleDotComGame.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
Help is much obliged.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That happens because locationCells when line 13 of SimpleDotCom is reached.

You declared the member variable locationCells in line 2, but you never initialize it to anything, so it's null.

Note that you're also not calling the setLocationCells from anywhere. I guess that's what you want to do in the main() method in class SimpleDotComGame.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig E. Lewis wrote:They as follows:

It looks like Jesper has answered your question, but congratulations on supplying a well thought-out one, with all the information we need to help you out. That's how to get questions answered.

Winston
 
Craig E. Lewis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I had a little more experience I think I would be able to solve the error based on jespers identification of the problem. But I am fairly new to Java and therefore I would like to know how I would solve the issue based on what you have told me. So if you do not mind give me a solution by changing a code. Thank you very much
Thank you Winston the compliment was much appreciated!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig E. Lewis wrote:If I had a little more experience I think I would be able to solve the error based on jespers identification of the problem. But I am fairly new to Java and therefore I would like to know how I would solve the issue based on what you have told me.

I don't know that there's anything I can add to what Jesper said. Perhaps you could explain what you don't understand?

So if you do not mind give me a solution by changing a code.

Not our style, I'm afraid. We help you out, and you find the solution.

Thank you Winston the compliment was much appreciated!

My pleasure. It's very refreshing to get a properly formulated question - especially in the 'Beginners' forum.

Winston
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as Jesper said you aren't providing your locationCells variable with a value, and that's the proximate cause of the error. But if you called the setLocationCells method, that would assign it a value.

And as Jesper said, you aren't calling that method anywhere, so that's the ultimate cause of the error. So to fix the error, perhaps calling that method would help. Jesper already suggested where you might call it from.
 
Craig E. Lewis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright Thanks guys for all the help I added a line calling upon the setLocationCells method after the boolean in the SimpleDotComGame class.
Again thanks for all your help.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!