This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Head First Jave - Code Example - NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Jave - Code Example - NullPointerException" Watch "Head First Jave - Code Example - NullPointerException" New topic
Author

Head First Jave - Code Example - NullPointerException

Michael Francis
Greenhorn

Joined: Sep 14, 2009
Posts: 2
I'm going through the Head First Java book, and I am working on an example code for a mini battleship game.

When I run the program(using eclipse) I get

Here are the three class files.







I also notice that in line 30 of GameHelper.java there is this line


however, alphcoords is never used throughout the program again. Could this be causing the error?

Thanks in advance!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You never initialize dotComsList. The NullPointerException tells you exactly on which line it occurs (DotComBust.java, line 19). Then you look on that line for any dereferencing. In this case, the line is dotComsList.add(one); and only dotComsList is dereferenced. Therefore it must be null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
The problem is in line 07:


dotComList is not initialized (is null), and program throws NullPointerException if you try to use it.
You must create an array before you can use it
Michael Francis
Greenhorn

Joined: Sep 14, 2009
Posts: 2
Thanks a lot, I should have caught that.

If there are moderators, this thread can be closed.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37943
    
  22
Welcome to JavaRanch

Aarr, there be moderators . . .

But we don't usually close threads. We simply let them drift off into the archives, in case somebody thinks of something useful to say later on.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Although of course not too much later
 
jQuery in Action, 2nd edition
 
subject: Head First Jave - Code Example - NullPointerException
 
Similar Threads
Head First Java Programs
Head First Java P148 - DotComBust - compile errors
Head First Java DotCom, DotComBust, GameHelper
game compile problems
DotComBustGame in HFJ -- not working; can't figure out what I've missed