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 Having NullPointerException Error in Connect4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Having NullPointerException Error in Connect4" Watch "Having NullPointerException Error in Connect4" New topic
Author

Having NullPointerException Error in Connect4

Jarom Knudsen
Greenhorn

Joined: Oct 14, 2010
Posts: 3
I'm having some trouble here that's really, really starting to frustrate me. I would be very grateful if anyone can help me understand what I am doing wrong.
This is supposed to be a connect4 game using all the pieces as objects. Somewhere along the line I screwed up.
IF anyone can help I would be extremely grateful!



Moderator Edit: Code tags added
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
A NullPointerException or NPE means that you're trying to de-reference a null, and often the cause can be found by carefully inspecting the offending object and trying to search for where it isn't being initialized. You already know the source, the PlayField array, and that the exception is occurring when calling GetPiece() on an item of this array. So the items of this array must be null, but how can this be when you initialize the array in the Board constructor?:


Well if you look a few lines up in the constructor you see this:


Do you see what you're doing? You're declaring a new Piece array called by the same name as the class field, PlayField, and so when you initialize the object referenced by this variable, you are only initializing and working with a variable local to the constructor not the one declared in the class, and this is your problem.

Solution, do not redeclare this variable in the constructor but rather use the variable declared in the class.

Also, please read up on UseCodeTags so that your code posts are readable (I have taken the liberty of editing your post and adding tags this time), and also read up on Java naming conventions since if you obey these, others, including your teachers and us, will be able to more readily understand your code.

Best of luck, and welcome to the Ranch!
Jarom Knudsen
Greenhorn

Joined: Oct 14, 2010
Posts: 3
Thank you so very, very much!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Jarom Knudsen wrote:Thank you so very, very much!


You're very very welcome!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Sorry for not noticing you are new. Welcome to JavaRanch
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Campbell Ritchie wrote:Sorry for not noticing you are new. Welcome to JavaRanch


We did notice it in my first post.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
pete stein wrote: . . . We did notice it in my first post.
Yes, so you did, but I didn't notice that you did notice.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having NullPointerException Error in Connect4
 
Similar Threads
Need some help, pleeaaase!!
Stymied on tic tac toe project
what's wrong with the code?
Please help, java program terminating unexpectedly without reason
how to access instance variables