• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initiated object returning null  RSS feed

 
Alex Vestin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I've been assigned to create a Black Jack game with a gui.
In this game I've created a seperate Player and Dealer class, and both initiated them, however when I try to call a Player object in a certain way I get a null pointer reference. (It should be noted that the Player object is an array)



And this is how I've initilized the Player class in the constructor



What I don't get is if I change players[i] to dealer, it works fine.

Thanks in advance!
 
Alex Vestin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems I needed to remove the "Player" at line 1 in the constructor, I'll remove the topic!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is line 1 of the 2nd code snippet really in the constructor? If so, then "players" refers to a different object than the object of the same name in line 6 of the 1st snippet (which would thus never haven been initialized, making a NPE the natural consequence).

Maybe you have an instance variable called "players", and meant line 1 of snippet 2 to read "players = new Player[numberofplayers]" instead?
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Alex Vestin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And welcome to the Ranch


Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!