-1
int playerNum,cardNum;
Cards card=null; 
        
for(playerNum=0;playerNum<=NUMPLAYERS-1;playerNum++)
{ 
    //some code...
            
    for(cardNum=1;cardNum<=5;cardNum++)
    {
     card=( deckOfCards.deal() ); 
     players[playerNum].setHand_1by1(card);//I get NullPointerException for using the method here
                
     //some code...
                    
    }
}

class Hand
 {
     private Cards[] hand=new Cards[5];
     private int counter;
     
     //no constructor, just a setter.
     public void setHand_1by1(Cards card)
     {
         if(counter>=hand.length)
         {
             System.out.printf("Hand is full");
         }
         else
         {
             hand[counter]=card;
             counter++;
         }
        
     }
//some code...
}

I understand that you get a NullPointerException when you try to dereference something that's pointing to nothing(ie null).

1)But card of Cards type is pointing to something due to the method deckOfCards.deal() so that's not the problem, must be something in the method.

2)In the setHand_1by1(), hand[counter] is of Cards type. All I did was make it point to valid memory containing data of Cards type in the else block.

Why am I then getting a NullPointerException error?

Edit: Just showing how I created my players:

Hand[] players=new Hand[NUMPLAYERS];
HelloWorld
  • 193
  • 2
  • 8
  • 1
    How about `players[...]`? – jumping_monkey Jun 27 '20 at 00:58
  • You access `players`. And either that array or one of the elements accessed by `players[playerNum]` is null. But you're not showing how you populate that array, so there's no telling. – QBrute Jun 27 '20 at 01:04
  • `players` is of `Hand` type. I'm populating it using the `setHand_1by1` method and get the error on the first try – HelloWorld Jun 27 '20 at 01:08
  • No, in `setHand_1by1` you're populating each `Hand`'s array of `Card`s, but not the `player` array outside that contains all `Hand`s. Show *that* code. – QBrute Jun 27 '20 at 01:11
  • You only create the `players` array, but you never put any `Hand` object in it. So all the elements are still `null` – QBrute Jun 27 '20 at 01:14
  • I assume `players[playerNum]` is `null`. Do you create the Hand objects anywhere? – Johannes Kuhn Jun 27 '20 at 01:16
  • @QBrute @Johannes, you are absolutely right, players[playerNum] points to `null`. The concept of array of objects is tripping me. Could someone convert that into an answer? Thanks – HelloWorld Jun 27 '20 at 01:25

1 Answers1

1

You need a Hand object reference in players[playernum] to be able to call a Hand method on it.

The players array that you created(of type Hand), is really just an array that can hold Hand(or its subclass') object references. Initially, the references are all null, and you are required to populate them yourself.

About your array of objects comments, note that there isn't ever an array of objects, only an array of object references. The references point to places where the object actually resides(usually the heap).

Ayush
  • 1,510
  • 11
  • 27