Question

EDIT: My first Question was resolved, however, the function getValue() always returns 11. Any thoughts?

EDIT: Found it,

Collections.shuffle(Arrays.asList(deck));

Isn't properly shuffling. I don't know why.

I'm doing an assignment where I have to write/fill in some code to write a simple blackjack program.

There are three classes I have defined, a Card Class, a Hand Class (which is an array of Cards and some other values) and a Deck class which is also an array of Cards.

In the Hand class I have a method that adds up all the values of the cards, and whenever I run my code, I get a java.lang.NullPointerException, and the console points to my this "addHand" method:

public int addHand(){
   int j=0;
   for(int i=0; i<=counter-1; i++){
      j += Cards[i].getValue();
   }

   return j;

}

I think it is because some of the values in Cards[] are null. But the "counter" should not let any of them get added. I'll include the rest of the Hand Class, as well as the Card Class.

Hand Class:

public class Hand {

private Card[] Cards;
private int counter;
//private int valueOfHand;

public Hand(){
    Cards = new Card[10];
    counter = 0;

}

public int numCards(){
   return counter;
}

public Card getCard(int i){
   return Cards[i];
}

public int getCounter() {
   return counter;
}

public void setCounter(int counter) {
   this.counter = counter;
}

public void addCard(Card card){
   Cards[counter] = card;
   setCounter(counter+1);
}

public int addHand(){
    int j=0;
    for(int i=0; i<=counter-1; i++){
      j += Cards[i].getValue();
   }

   return j;

}



}

And the Card Class:

public class Card {

private String value;
private String suit;

public Card(String number, String shape){
    value = number;
    suit = shape;

}
public int getValue() {
    if (value.equals("King") || value.equals("Queen") || value.equals("Jack")){
        return 10;
    }
    else if (value.equals("Ace")){
        return 11;
    }
    else {
        return Integer.parseInt(value);
    }


}
public void setValue(String value) {
    this.value = value;
}
public String getSuit() {
    return suit;
}
public void setSuit(String suit) {
    this.suit = suit;
}
}

I don't know if I need to post anything else, I would be happy to if necessary.

The exact error is:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at Hand.addHand(Hand.java:39)
at BlackJackGUIPanel$CardPanel.hitMe(BlackJackGUIPanel.java:182)
at BlackJackGUIPanel$CardPanel.actionPerformed(BlackJackGUIPanel.java:161)

Here is the Hitme code:

void hitMe() {
     if (!gameInProgress) {
        message = "Click \"New Game\" to start a new game!";
        repaint();
        return;
     }


     player.addCard(deck.giveCard());
     if(player.addHand() > 21){
         message = "Game over, you busted";
         gameInProgress = false;
     }


 message = "You clicked Hit Me!";
     repaint();

and the giveCard() code:

public Card giveCard(){
    Card temp = Decks[0];
    for(int i = 1; i<52; i++){
        Decks[i-1] = Decks[i];
    }
    Decks[51] = temp;
    return temp;

}

I couldn't find a better way to do this part. here is where I initialize my deck:

public Deck(){
    Decks = new Card[52];


    //int cardNumber = 2;
    for(int i = 0; i < 52; i += 4){
        switch (i/4){
        case 0: Decks[i] = new Card("2", "Spades");
                Decks[i+1] =  new Card("2", "Hearts");
                Decks[i+2] = new Card("2", "Clubs");
                Decks[i+3] = new Card("2", "Diamonds");

        case 4: Decks[i] = new Card("3", "Spades");
                Decks[i+1] =  new Card("3", "Hearts");
                Decks[i+2] = new Card("3", "Clubs");
                Decks[i+3] = new Card("3", "Diamonds");

        case 8: Decks[i] = new Card("4", "Spades");
                Decks[i+1] =  new Card("4", "Hearts");
                Decks[i+2] = new Card("4", "Clubs");
                Decks[i+3] = new Card("4", "Diamonds");

        case 12: Decks[i] = new Card("5", "Spades");
                Decks[i+1] =  new Card("5", "Hearts");
                Decks[i+2] = new Card("5", "Clubs");
                Decks[i+3] = new Card("5", "Diamonds");

        case 16: Decks[i] = new Card("6", "Spades");
                Decks[i+1] =  new Card("6", "Hearts");
                Decks[i+2] = new Card("6", "Clubs");
                Decks[i+3] = new Card("6", "Diamonds");

        case 20: Decks[i] = new Card("7", "Spades");
                Decks[i+1] =  new Card("7", "Hearts");
                Decks[i+2] = new Card("7", "Clubs");
                Decks[i+3] = new Card("7", "Diamonds");

        case 24: Decks[i] = new Card("8", "Spades");
                Decks[i+1] =  new Card("8", "Hearts");
                Decks[i+2] = new Card("8", "Clubs");
                Decks[i+3] = new Card("8", "Diamonds");

        case 28: Decks[i] = new Card("9", "Spades");
                Decks[i+1] =  new Card("9", "Hearts");
                Decks[i+2] = new Card("9", "Clubs");
                Decks[i+3] = new Card("9", "Diamonds");

        case 32: Decks[i] = new Card("10", "Spades");
                Decks[i+1] =  new Card("10", "Hearts");
                Decks[i+2] = new Card("10", "Clubs");
                Decks[i+3] = new Card("10", "Diamonds");

        case 36: Decks[i] = new Card("Jack", "Spades");
                Decks[i+1] =  new Card("Jack", "Hearts");
                Decks[i+2] = new Card("Jack", "Clubs");
                Decks[i+3] = new Card("Jack", "Diamonds");

        case 40: Decks[i] = new Card("Queen", "Spades");
                Decks[i+1] =  new Card("Queen", "Hearts");
                Decks[i+2] = new Card("Queen", "Clubs");
                Decks[i+3] = new Card("Queen", "Diamonds");

        case 44: Decks[i] = new Card("King", "Spades");
                Decks[i+1] =  new Card("King", "Hearts");
                Decks[i+2] = new Card("King", "Clubs");
                Decks[i+3] = new Card("King", "Diamonds");

        case 48: Decks[i] = new Card("Ace", "Spades");
                Decks[i+1] =  new Card("Ace", "Hearts");
                Decks[i+2] = new Card("Ace", "Clubs");
                Decks[i+3] = new Card("Ace", "Diamonds");
        }
        }
    Collections.shuffle(Arrays.asList(Decks));
    }
Was it helpful?

Solution

As a debug you could do this

public int addHand()
{
   int j=0;
   for(int i=0; i<=counter-1; i++)
   {
      Card c = Cards[i];
      if(c != null)
         j += Cards[i].getValue();
      else
         System.out.println("Add hand was trying to add a null card for index = " + i);
   }
   return j;
}

You may also want to add a check to your addCard(Card) method so that null cards cannot be added

public void addCard(Card card)
{
    if(card != null)
    {
        Cards[counter] = card;
        setCounter(counter+1);
    }
    else
        System.out.println("NO NO NO! Trying to add a null card to your hand");
}

OTHER TIPS

It depends on call to addCard method. This method is setting card to your cards variable. Suppose you called addCard 3 times, then 0, 1 and 2 iteration of the loop in addHand will be ok but iteration 3 will through exception because cards[3] is null.

Make sure you properly add cards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top