java coding problem: hangs on a arraylist add
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.
精彩评论