2014-01-21 35 views
0

我有一小段代碼設計用來在單擊按鈕時繪製卡片。截至目前,當我激活actionPerformed()時,出現以下錯誤(其中包括):線程「AWT-EventQueue-0 java.lang.IndexOutOfBoundsException ...」中的異常。我完全陌生,所以原諒我,如果這是一個簡單的錯誤。來自ActionListener的Java調用方法

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; /*Used for Random and ArrayList*/ 
public class AL extends Frame implements WindowListener,ActionListener { 
    TextField text = new TextField(20); 
    Button b; 
    private int numClicks = 0; 
    private static ArrayList<Card> deck=new ArrayList<Card>(); 
    private static ArrayList<Card> playerOneDeck=new ArrayList<Card>(); 
    private static ArrayList<Card> playerTwoDeck=new ArrayList<Card>(); 



    public static void main(String[] args) { 
      AL myWindow = new AL("Well of course you know, this means WAR!"); 
      myWindow.setSize(350,100); 
      myWindow.setVisible(true); 
      ArrayList<Card> playerTwoDeck=new ArrayList<Card>(); 
    }//end main() 

    public AL(String title) { 

      super(title); 
      setLayout(new FlowLayout()); 
      addWindowListener(this); 
      b = new Button("Draw"); 
      add(b); 
      add(text); 
      b.addActionListener(this); 
      ArrayList<Card> deck=new ArrayList<Card>(); 
      ArrayList<Card> playerOneDeck=new ArrayList<Card>(); 
      deck=initializeDeck(); 
      Collections.shuffle(deck); 
    } 

    public void actionPerformed(ActionEvent e) { 
      numClicks++; 
      Card theCard=playerOneDeck.get(0); 
      text.setText(theCard.name); 
      text.setText(theCard.name); 
    } 

    public void windowClosing(WindowEvent e) { 
      dispose(); 
      System.exit(0); 
    } 
    /*Assign names, values, and suits to all of the Card objects.*/ 
    public static ArrayList<Card> initializeDeck() { 
     Card[] tempDeck=new Card[52]; 
     ArrayList<Card> completeDeck=new ArrayList<Card>(); 
     for (int i=0; i<tempDeck.length; i++) { 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Two"; 
      tempDeck[i].value=2; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Three"; 
      tempDeck[i].value=3; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Four"; 
      tempDeck[i].value=4; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Five"; 
      tempDeck[i].value=5; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Six"; 
      tempDeck[i].value=6; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Seven"; 
      tempDeck[i].value=7; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Eight"; 
      tempDeck[i].value=8; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Nine"; 
      tempDeck[i].value=9; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Ten"; 
      tempDeck[i].value=10; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Jack"; 
      tempDeck[i].value=11; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Queen"; 
      tempDeck[i].value=12; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="King"; 
      tempDeck[i].value=13; 
      i++; 
      tempDeck[i]=new Card(); 
      tempDeck[i].name="Ace"; 
      tempDeck[i].value=14; 
     }//end FOR 
     /*Assign suits*/ 
     /*keep in mind that the array is now [2], [3],..[k],[a],[2],[3],[k],[a],etc */ 
     for (int j=0; j<tempDeck.length; j++) { 
      tempDeck[j].suit="Hearts"; 
      j++; 
      tempDeck[j].suit="Diamonds"; 
      j++; 
      tempDeck[j].suit="Spades"; 
      j++; 
      tempDeck[j].suit="Clubs"; 
     }//end FOR 

     for (int k=0; k<tempDeck.length;k++) { 
      completeDeck.add(tempDeck[k]); 
     } 
     return completeDeck; 
    }//end initializeDeck() 

    public void windowOpened(WindowEvent e) {} 
    public void windowActivated(WindowEvent e) {} 
    public void windowIconified(WindowEvent e) {} 
    public void windowDeiconified(WindowEvent e) {} 
    public void windowDeactivated(WindowEvent e) {} 
    public void windowClosed(WindowEvent e) {} 

} //結束AL類

+2

1)爲了更好地提供幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)。 2)對代碼塊使用一致的邏輯縮進。代碼的縮進旨在幫助人們理解程序流程。 3)通常不需要使用'static',並且代碼味道不好。 –

+0

堆棧跟蹤說哪一行發生異常? – Kayaman

+0

順便說一句 - 爲什麼是AWT而不是Swing?看到我對[Swing extras over AWT]的回答(http://stackoverflow.com/a/6255978/418556)有很多很好的理由放棄使用AWT組件。 –

回答

1

您嘗試在ActionPerformed第一行從PlayerOneDeck獲得的第一個元素:Card theCard=playerOneDeck.get(0);然而,PlayerOneDeck是空ArrayList。它沒有元素,所以試圖從中得到一個元素返回IndexOutOfBoundsException

您初始化一個類變量PlayerOneDeck並且還有一個變量在您的同名構造函數中。您可以使用元素填充構造函數變量,但actionPerformed將調用保留爲空的類變量。例如:

public class Main { 
    ArrayList<Integer> array = new ArrayList<Integer>(); // class variable 

    public static void main(String[] args) { 
     ArrayList<Integer> array = new ArrayList<Integer>(); // method variable, overrides class variable within the main method 
     array.add(1); // adds 1 to the method variable 
     (new Main()).printArray(); 
    } 

    public void printArray() { 
     System.out.println(array.get(0)); // tries to print the first element of class variable, throws IndexOutOfBoundsException 
    } 
} 

刪除您的本地變量並僅使用全局類,並且您的問題可能會消失。

+0

我的歉意是,我複製並粘貼了一箇舊版本。 PlayOneDeck現在有內容。 – eggHunter

+0

我有一個無GUI的版本,工作正常,所以我知道邏輯是正確的。 – eggHunter

+1

我注意到你有一個類變量'PlayerOneDeck',並且還在'main'方法中實例化了一個名字相同的變量。你可能試圖用元素填充該方法變量,但是'actionPerformed'使用類實例變量,它將保持爲空。 – asaini007