• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why am I getting a null pointer exception?  RSS feed

 
Bennett Nelson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys! I am on chapter 5 of "Head First Java" (feels like I have been forever) where you build a simple version of a battleship game. I have a class with a method that checks to see if the guess is a hit, and then I have my main class. Right now I am using my main method to see if my checker method is working correctly. It compiles fine, but when I try to run the class with my main method, it gives me this:



Do you guys know why this is happening? I have closely compared my code to that of the book. I do have some different variable and array names but I made sure that everything matches up. Did I miss something? Maybe I'm just tired? My classes are below. Sorry for a long post.

SimpleDotCom (contains my checking method)


SimpleDotComTestDrive (supposed to test SimpleDotCom)
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hint: Declaring an array is not the same as initializing it
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...have a look at your setHitLocations method.

Manish
 
Bennett Nelson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry. i'm really confused. Shouldn't my SimpleDotComTestDrive class initialize it using the setHitLocations method? With the elements of the locations array? Or maybe I'm not sure what initializing really means XD

Would this fix it: int[] hitLocations = new int[];
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look carefully at this:


You pass a reference in to the method, calling it hitLocs. Then, you assign it to point to something else.

Is that REALLY what you want to do? For all intents and purposes, this method does nothing.
 
Bennett Nelson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. Yeah I pretty much feel like the biggest idiot on the face of the planet XD Ok so instead of this:



It should be like this right:



Hard for me to forget that order DOES matter for =, but not for ==. Thanks for the help guys!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!