java编码问题:挂在arraylist上添加

时间:2011-02-06 20:46:00

标签: java nullpointerexception

我在arraylist之前用作结构,但在这段代码中它不起作用。 有人可以帮助我,因为我找不到错误? (我确定这是我的错,但IDE什么也没说)

流程: 第一堂课比赛。我打电话给runGame并且它正好流动直到 Hand hand = new Hand(this.deck); (这是对签署问题的权利的评论

public class Game {

        private ArrayList<Player> playerArray;
    private int maxPlayers;
        private Deck deck;

    //constructor
    public Game(ArrayList playerArray, int maxPlayers)
    {
        this.playerArray = playerArray;
        this.maxPlayers = maxPlayers;
        }

    // game method for the match
    public  void runGame()
        {
            //shuffle of players
            Collections.shuffle(this.playerArray);

            //creation of the deck
            this.deck = new Deck();
            System.out.println(new java.util.Date().toString() +"  "+"deck created");

            //shuffle the deck 
            this.deck.shuffleDeck();
            System.out.println(new java.util.Date().toString() +"  "+"deck shuffled");


            // distribuiting the hands to all players
            //and preventing them to send something
            for (int i = 0; i < this.maxPlayers; i++)
            {
                Player currentPlayer = this.playerArray.get(i);
                Socket socket = currentPlayer.getConnection();

                Hand hand = new Hand(this.deck);// the problem starts here  comes form the constructor of the hand
                 System.out.println(" after hand ");

                sendingBlockString(socket, currentPlayer); //send the block string
                sendingHand(socket, currentPlayer, hand );//send the hand
            }

问题显然在Hand类中的手工构造函数中,它在循环中挂起,exaclty试图添加甲板的弹出汽车(deck.popCard()函数已经过测试并完美运行所以它不是阻止add()函数)我从来没有到达第二个system.out.println代码:

public class Hand  implements Serializable
{

    private ArrayList<Card> theHand;
    private Player player;
    private int handValue ;  // from 1  to 10 


        public Hand(Deck deck)
        {
            for (int i=0; i<4; i++)
            { 
                System.out.println("before popping deck");
                this.theHand.add(i, deck.popCard());// adding the card taken from the deck (5 times)  //// this is the problem  it hangs!
                System.out.println("after add to hand");
            }

         }

2 个答案:

答案 0 :(得分:1)

你确定它挂了吗?它应抛出NullPointerException,因为您的ArrayList未初始化。

答案 1 :(得分:1)

如果没有卡片,我会怀疑dec.popCard()会阻止。 <{1}}方法本身无法挂起。