File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 19693
    
  20

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: 38818
    
  23
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: 19693
    
  20

Although of course not too much later
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Jave - Code Example - NullPointerException