Java简单的BlackJack,java.lang.NullPointerException

时间:2013-10-01 03:37:33

标签: java class blackjack

编辑:我的第一个问题已经解决,但是函数getValue()总是返回11.有什么想法吗?

编辑:发现它,

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

没有正确洗牌。我不知道为什么。

我正在完成一项任务,我必须编写/填写一些代码来编写简单的二十一点程序。

我定义了三个类,一个Card类,一个Hand Class(一个Cards数组和一些其他值)和一个Deck类,它也是一个Cards数组。

在Hand类中,我有一个方法可以将卡的所有值相加,每当我运行代码时,我都会得到一个java.lang.NullPointerException,并且控制台指向我的“addHand”方法:< / p>

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

   return j;

}

我认为这是因为Cards []中的某些值为null。但“反击”不应该让任何一个加入。我将包括其他手工课程以及卡片类。

手类:

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;

}



}

卡片类:

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;
}
}

我不知道是否需要发布任何其他内容,如果有必要,我会很高兴。

确切的错误是:

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)

以下是Hitme代码:

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();

和giveCard()代码:

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

}

我找不到更好的方法来做这个部分。 这是我初始化我的套牌的地方:

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));
    }

2 个答案:

答案 0 :(得分:0)

作为调试你可以做到这一点

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;
}

您可能还想在addCard(Card)方法中添加支票,以便无法添加空卡

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");
}

答案 1 :(得分:0)

这取决于对addCard方法的调用。此方法是将卡设置为cards变量。假设您调用了addCard 3次,那么addHand中的循环的0,1和2迭代将是正常的,但迭代3将通过异常,因为cards[3]null。< / p>

确保正确添加卡片。