So I have an abstract class, Player. Then I have classes, thePlayer, and Dealer, which extend Player. I have a method initHand, which is an array of BlackJackCard. in thePlayer and Dealer I call this method to assign to my own array to use. However, I get null exceptions as if there are no cards in the array, meaning somethings wrong but what I've done seems to make logical sense.
public BlackJackCard[] initHand(BlackJackDeck myDeck)
{
PlayerHand = new BlackJackCard[x];
PlayerHand[0] = mydeck.deal();
PlayerHand[1] = mydeck.deal();
return PlayerHand;
}
and then my ThePlayer class
public BlackJackCard[] initHand(BlackJackDeck adeck)
{
return super.initHand(adeck);
}
public BlackJackCard[] PlayerHand = initHand(deck);
EDIT: so since formatting in comments is weird i'll do this here. Here is my main method, where I create myDeck and feed it too my ThePlayer and Dealer classes.
myDeck = new BlackJackDeck();
myDeck.createDeck();
myDeck.shuffleDeck();
thePlayer myPlayer = new thePlayer(myDeck);
Dealer theDealer = new Dealer(myDeck);
Then in thePlayer I have my constructor
BlackJackDeck deck;
public thePlayer(BlackJackDeck mydeck) {
deck = mydeck;
}
the error it gives
Exception in thread "main" java.lang.NullPointerException
at blackjack.thePlayer.presenthand(thePlayer.java:79)
at blackjack.Blackjack.main(Blackjack.java:36)
Java Result: 1
line 79=`@Override public String presenthand() { String myhand = "";
for (int i = 0; i < currentCard; i++) {
(79)myhand += (PlayerHand[i].getSuit() + " of " + PlayerHand[i].getNum() + " ");
}
return myhand;
}`
and then line 39 is the line in the main class where I call presenthand