File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having NullPointerException Error in Connect4

 
Jarom Knudsen
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so very, very much!
 
pete stein
Bartender
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jarom Knudsen wrote:Thank you so very, very much!


You're very very welcome!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47274
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for not noticing you are new. Welcome to JavaRanch
 
pete stein
Bartender
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Sorry for not noticing you are new. Welcome to JavaRanch


We did notice it in my first post.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47274
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic