2012-11-15 36 views
2

我正在研究紙牌計數程序。我已經有主程序工作,但是當我嘗試實現自己的類時,我在第19行出現NullPointerException錯誤(每當它到達c.getRank時)。執行自己的類java時出現NullPointerException錯誤

請注意,我在導入一個名爲CardDeck的類時首先創建了我的主程序,該類具有所需的所有功能,但現在我應該創建自己的類來完成完全相同的功能。 (請注意,我無權訪問導入的CardDeck課程)。

下面是主要代碼:

import se.lth.cs.ptdc.cardGames.Card; 

public class Patiens { 
public static void main(String[] args) { 
    double good = 0; 
    double bad = 0; 
    double result = 0; 

    for (int a = 0; a < 1000000; a++) { 
     CardDeck deck = new CardDeck(); 
     deck.shuffle(); 
     double fail = 0; 
     while (deck.moreCards()) { 

      for (int i = 1; i <= 3 && deck.moreCards(); i++) { 

       Card c = deck.getCard(); 

       if (i == 1 && c.getRank() == 1) { 
        fail++; 
       } 

       if (i == 2 && c.getRank() == 2) { 
        fail++; 
       } 

       if (i == 3 && c.getRank() == 3) { 
        fail++; 
       } 
      } 
     } 
     if (fail >= 1) { 
      bad++;  
     } 
     else{ 
      good++; 
     } 
    } 
    System.out.println("Good: " + good + " Bad: " + bad); 
    result = good/bad; 
    System.out.println("Result= " + result); 
} 

} 

它是什麼算我的甲板將成功完成的概率:

它的票1-2-3,1-2-3,而在同時畫一張卡片。現在如果卡片恰好是一個ACE,當它計數爲「1」時,當前卡片將失敗。等級爲2的牌同樣適用於「2」等等。無失敗完成的概率爲0.8%。

這裏是CardDeck類我創建:

import se.lth.cs.ptdc.cardGames.Card; 

import java.util.Random; 

public class CardDeck { 
    private Card[] cards; 
    private int current; 
    private static Random rand = new Random(); 

    public CardDeck() { 
     cards = new Card[52]; 
     for(int suit = Card.SPADES; suit <= Card.CLUBS; suit++) { 
      for (int i = 0; i < 13; i++) { 
       cards[i * suit] = new Card(suit, i); 
      } 
     } 
     current = 0; 
    } 

    public void shuffle() { 
     Card k; 
     for(int i = 1000; i > 0; i--) { 
      int nbr = rand.nextInt(52); 
      int nbr2 = rand.nextInt(52); 
      k = cards[nbr2]; 
      cards[nbr2] = cards[nbr]; 
      cards[nbr] = k; 
     } 
    } 

    /** 
    *Checks for more cards 
    */ 
    public boolean moreCards() { 
     if(current > 51) { 
      return false; 
     } else { 
      return true; 
     } 
    } 

    /** 
    *Draws the card lying on top. 
    */ 
    public Card getCard() { 
     return cards[current++]; 

    } 
} 

這裏是import se.lth.cs.ptdc.cardGames.Card;如果需要的話,它是創建卡類。

package se.lth.cs.ptdc.cardGames; 

public class Card { 
    public static final int SPADES = 1; 
    public static final int HEARTS = SPADES + 1; 
    public static final int DIAMONDS = SPADES + 2; 
    public static final int CLUBS = SPADES + 3; 

    private int suit; 
    private int rank; 

    public Card(int suit, int rank) { 
     this.suit = suit; 
     this.rank = rank; 
    } 

    public int getSuit() { 
     return suit; 
    } 

    public int getRank() { 
     return rank; 
    } 
} 

(請注意,我不應該改變上面的類)

+0

如果您要求調試excpetion,請始終包含堆棧跟蹤。 –

+0

在'getCard()'方法中爲'current'計數並檢查哪個索引Card對象爲null – vels4j

+0

或者只是學會使用調試器,這應該非常簡單調試 – Zharf

回答

4

你的問題是在這裏:

cards[i * suit] = new Card(suit, i); 

如果你改變它:

cards[i + ((suit - 1) * 13)] = new Card(suit, i); 

它會做你所期望的。

二要考慮的事情:首先,數組是從零開始,所以你的第一張牌必須是在索引0其次,由西裝相乘,你會得到這個數字,例如倍數:

  • SPADES:1,2,3,4,5,6,7,8,9,10,11,12,13
  • HEARTS:2,4,6,8,10 ...
  • DIAMONDS:3,6,9,12 ...
  • CLUBS:4,8,12,16 ...

因此,一些元素將被填充多次(12次填充四次),並且一些(特別是素數> 13)元素(例如, 23)將爲空。一般來說,用另一個變量來表示索引可能就足夠了,如下所示:

int cardIndex = 0; 
for (int suit = Card.SPADES; suit <= Card.CLUBS; suit++) { 
    for (int i = 0; i < 13; i++) { 
     cards[cardIndex++] = new Card(suit, i); 
    } 
} 
+0

作爲回報,這給了我一個java.lang.ArrayIndexOutOfBoundsException – Rob

+0

好吧,非常感謝。有效。雖然我會在這方面多讀一點(對於Java相當新穎),並嘗試其他人提出的其他建議:)這是一個相當簡單的解決方案(我一直都是這樣),在這個解決方案上我花了很多時間。 – Rob

0

看來你CardDeck類的getCard()方法返回的null而不是返回卡對象。檢查getCard()方法並在返回之前打印Card obj。

1

數組CardDeck.cards包含null元素,因爲i * suit不會做你期望什麼,當你使用基於1的索引(你在做Card.SPADES

+0

OP應該不會更改卡類 – BigMike

+0

不完全:0將爲空,其次是數字不能被14和52之間的1,2,3和4整除。 –

+0

已修復我的答案。 @BigMike:那麼他需要修復'CardDeck'。 –

相關問題