• 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

Problem with SimpleDotComGame from the book Head First Java.

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic