-2

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

  • 2
    what is null? how is you code called? what is `x`? If you were given this code and description do you think you could understand what is wrong? – Scary Wombat Sep 08 '14 at 04:25
  • looks like mydeck.deal() would be causing NPE. how are you passing deck to initHand? – Manjunath Sep 08 '14 at 04:27
  • mydeck is created in my main tread, then when my ThePlayer and Dealer classes are created it passes the mydeck variable to them in the constructor. X is just a placeholder int for the max hand size. – user3316794 Sep 08 '14 at 04:32
  • @user3316794 your code looks proper. Can you print the stacktrace and any line number where you are seeing this nullpointer exception? – Manjunath Sep 08 '14 at 04:54
  • @user3316794 It looks like you have initialized the array correctly as you are not getting ArrayIndexOutOfBoundException. Check your deal() method in mydeck.deal() . Is it returning a valid BlackJackCard object or is it returning a null reference which I believe is the cause of NPE – Manjunath Sep 08 '14 at 05:08
  • My Deal method should work, as I haven't changed the class since an earlier version which would work fine. – user3316794 Sep 08 '14 at 05:54

1 Answers1

0

What value of deck are passing to below line of code,

public BlackJackCard[] PlayerHand = initHand(deck);

If deck is null then The line

PlayerHand[0] = mydeck.deal();

will throw Null pointer exception.

dReAmEr
  • 6,986
  • 7
  • 36
  • 63