Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Jave - Code Example - NullPointerException

 
Michael Francis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, I should have caught that.

If there are moderators, this thread can be closed.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50251
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although of course not too much later
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic