Java array, NullPointerException?
I declared two cards:
Card card1 = new Card('3', Card.Suit.clubs);
Card card2 = new Card('T', Card.Suit.diamonds);
This works:
Hand hand1 = new Hand();
hand1.takeCard(card1);
But why does this not work? It gives me a NullPointerException on second line:
Hand[] hand = new Hand[2];
hand[0].takeCard(card2);
Solution 1:
You are declaring an array of 2 hands. This is just setting up the array. You then need to instantiate the hand objects inside the array.
Say
hand[0] = new Hand();
hand[1] = new Hand();