Java中的编码,初始化卡片组的问题

时间:2018-03-31 21:35:46

标签: java nullpointerexception bridge playing-cards

我正在编写应该初始化一副牌的代码,将它们洗牌,然后将它们交给特定数量的玩家。我已经编写了所有必要的方法,但是当我尝试实现它们时,它会编译,但是当我运行它时,我得到一个源自我的shuffle方法的NullPointerException。

我在思考的是,在我的牌组初始化中出现了问题,因此当我尝试在后续方法中访问牌组时,我得到了例外。

我不确定出了什么问题,非常感谢你的帮助! 这是代码:

我在Shuffle方法中尝试切换卡片时出现异常

import java.util.Random;
import java.util.Arrays;

public class Deck
{


   private Card [] arrCards;

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

     String [] suits = {"Hearts", "Spades", "Diamonds", "Clubs"};

     int count = 0;

     for(int i = 0; i < suits.length; i++)
     {
       for(int j = 1; j <= 13; j++)
       {

         Card deckCards = new Card(suits[i], j);

         deckCards.print();

         arrCards[count] = deckCards;

         count++;

       }


     }


   }

  public void shuffle()
  {

   int seed = 123;

   //create arbitrary new temp card

   Card temp = new Card("Hearts", 1);

   Random randNum = new Random(seed);


   for(int i = 0; i < 1000; i++)
   {

     int firstRand = randNum.nextInt(52);

     int secondRand = randNum.nextInt(52);


     temp = arrCards[firstRand];
     arrCards[firstRand] = arrCards[secondRand];
     arrCards[secondRand] = temp;

   }


 }

 public Card [] dealHand(int n, int playerNum)
 {

    Card [] playerCards = new Card[n];


    int counter = 0;

    int start = (n  * (playerNum - 1));

    int end = start + 12;

    for(int i = 0; i < n; i++)
    {
      for(int j = start; j < end; j++)
      {
       playerCards[i] = arrCards[j];

       }

     }

   return playerCards;
 }

}

非常感谢,我真的很感激! (特别是因为这可能是某个地方真正的基本错误)

:)

0 个答案:

没有答案