2011-02-06 9 views
0

私は構造体として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"); 
      } 

     } 
+1

'theHand'フィールドはどこかで初期化されていますか?そうでなければ 'this.theHand.add'を呼び出すと' NullPointerException'が発生します。 – Piotr

+2

取得しているエラーメッセージは何ですか? – Davidann

+3

ハンドはどこに初期化されていますか? NullPointerExceptionが発生しないため、theHandを初期化するコードを省略する必要があります。また、deck.popCard()が有罪ではないと確信していますか?あなたもその方法を投稿する方がいいでしょう。 – Mnementh

答えて

1

は、あなたがそれてください。 ArrayListが初期化されていないので、NullPointerExceptionがスローされます。

+0

はい!私の領域の天才が、私はその瞬間にそれを記入して作成する必要があるので、配列を初期化する方法??? – Mikeel

+0

あなたの質問のすべてのコメントで示唆されているように、実際には基本的なエラーです。 "theHand = new ArrayList ();"という行を追加するだけです。あなたのコンストラクタの最初の行に。しかし、1つの質問:この種類のエラーは、エラーをスローする、それをハングアップさせません...または別のスレッドでこのコードフローを実行していた可能性がありますか? – Dunaril

1

カードがなくなったらdec.popCard()ブロックと思われます。 addメソッド自体はハングアップできません。

+0

しかし、デッキがいっぱいで、私はthis.hand.add()の前にdeck.popCard()を実行し、addメソッドに渡すオブジェクトカードに割り当てられました..... mhhh私はさらにthxをチェックします。 !! – Mikeel