私は構造体としてarraylistの前に使用しましたが、このコードではうまくいきません。 私はエラーを見つけることができないので誰かが私を助けることができますか? (私は間違いだと思うが、IDEは何も言わない)Javaコーディングの問題:arraylistにハングアップ
流れ: 最初のクラスのゲーム。私はrunGameを呼び出すと、ポイントまでOKになります。ハンドハンド=新しいハンド(this.deck);(THERが問題
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
}
をsignaleする権利上のコメントである問題は、それがデッキのポップ車を追加しようとexaclty、のためのサイクルに掛かるクラスの手に手のコンストラクタで明らかに(deck.popCard()関数がテストされ、それが追加()関数をブロックすることじゃないので、完璧に動作している)私はここにコードを第二の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");
}
}
'theHand'フィールドはどこかで初期化されていますか?そうでなければ 'this.theHand.add'を呼び出すと' NullPointerException'が発生します。 – Piotr
取得しているエラーメッセージは何ですか? – Davidann
ハンドはどこに初期化されていますか? NullPointerExceptionが発生しないため、theHandを初期化するコードを省略する必要があります。また、deck.popCard()が有罪ではないと確信していますか?あなたもその方法を投稿する方がいいでしょう。 – Mnementh