Java array, NullPointerException?
•
Java
I declare 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 doesn't it work? It gives me a NullPointerException on the second line:
Hand[] hand = new Hand[2]; hand[0].takeCard(card2);
Solution
You are announcing a set of two hands This is just setting the array Then, you need to instantiate the hand object in the array
say
hand[0] = new Hand(); hand[1] = new Hand();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码