开发者

java coding problem: hangs on a arraylist add

开发者 https://www.devze.com 2023-02-09 11:23 出处:网络
i used before arraylist as structurebut in this piece of code it doesnt works. Can someone help mesince i cant find the error ? (i am sure it smy mistake but the IDE says nothing)

i used before arraylist as structure but in this piece of code it doesnt works. Can someone help me since i cant find the error ? (i am sure it s my mistake but the IDE says nothing)

the flow: first the class Game. i call th开发者_运维技巧e runGame adn it flows ok untill the point Hand hand = new Hand(this.deck); ( ther is a comment on the right to signale the problem

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
            }

The problem is clearly in the hand constructor in the class Hand where it hangs in the cycle for, exaclty trying to add the popped car of the deck ( the deck.popCard() function is tested and works perfectly so it s not that blocking the add() function ) i never reach the second system.out.println here the code:

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

         }


Are you sure it hangs ? It should throw a NullPointerException since your ArrayList is not initialized.


I would suspect that dec.popCard() blocks if there are no more cards. the add method itself can't hang.

0

精彩评论

暂无评论...
验证码 换一张
取 消