2012-02-13 77 views
1

我有一個問題,我失去了一個arrayList中的數據。在我調用類MPUComp中的Refresh方法後,我進入mpuChecker類並調用updateTextArea。從arraylist丟失數據

通過這樣做,我失去了MPUComp中arraylist中存在的數據。我做錯了什麼。我認爲這與我如何稱呼這個班有關。我如何正確保存這些數據?

public class MPUComp extends JFrame { 
{ 

private mpuChecker mC; 
public ArrayList<String> oldTags = new ArrayList<String>(); 

public void menu() 
{ 
//... 
class MenuActionListener3 implements ActionListener { 
    public void actionPerformed(ActionEvent e) 
    { 
     mC = new mpuChecker(); 
     mC.CheckMpu(path, textField.getText(),1); 
     setVisible(false); 
    } 
} 
class MenuActionListener4 implements ActionListener { 
    public void actionPerformed(ActionEvent e) 
    { 
     mC = new mpuChecker(); 
     mC.CheckMpu(path2, textField_1.getText(),2); 
     setVisible(false); 
    } 
} 

public void refresh(String pane1) { 
    textArea_1.append(pane1 + "\n"); 
    System.out.println(getOldTags().size()); 
    System.out.println(oldTags.size()); 
    //both print out zero when called second 
} 


public void updateTextArea(final String text) { 
     textArea_2.append(text + "\n"); 
     oldTags.add(text); 
     System.out.println(oldTags.size()); 
     //prints out the correct arraylist size 
} 
} 
} 

//second class which calls updateTextArea and refresh 

public class mpuChecker { 

    private MPUComp mC = new MPUComp(); 

public void CheckMpu(String path, String searchToken, int form) 
{ 
// Print the text to the appropriate text-area either 1 or 2 
public void ary1(int path) 
{ 
    if(path == 1) 
    { 
     for(int l = 0; l < midTags.size(); l++) 
    { 
     mC.refresh(midTags.get(l)); 
    } 
    } 
    if(path == 2) 
    { 
     for(int lk = 0; lk < midTags2.size(); lk++) 
    { 
     mC.updateTextArea(midTags2.get(lk)); 
    } 
    } 
} 
} 
+0

setOldTags()是做什麼的? – jlewis42 2012-02-13 23:35:45

+0

那不重要我刪除了它我試圖使用getters/setters – 2012-02-13 23:38:05

+0

這裏有一堆未定義的變量,例如checkmpu(..)中的midTags;你能否在這些內容上添加細節?我不太瞭解你的代碼,對於您未打印相應方法的方法調用也是如此。 – 2012-02-13 23:43:10

回答

1

什麼JPM跟進建議,爲了避免這個,你可以做

private MPUChecker mC = new MPUChecker(); 
然後

在MPUComp。這樣,你只實例化mpuchecker一次。雙方的ActionListeners可以使用mPUChecker。

如果希望每個的ActionListener有自己的mPUChecker,你可以聽衆的創作進入者的身體內部班級,像這樣

class MenuActionListener3 implements ActionListener { 
    MPUChecker menu3mC = new mpuChecker(); 

    public void actionPerformed(ActionEvent e) 
    { 
     menu3mC.CheckMpu(path, textField.getText(),1); 
     setVisible(false); 
    } 
} 

另一方面,MPUChecker本身可能指的是錯誤的MPUComp,因爲您在初始化該對象時爲MPUChecker創建了一個。除非這是預期的行爲,你可以刪除

private MPUComp mC = new MPUComp(); 
從MPUChecker

,使CheckMPU靜態的,並給它額外的參數:它應該檢查MPUComp。

+0

私有靜態MPUComp mC = new MPUComp();在mpuchecker類固定我的問題謝謝你!但我不確定爲什麼。 – 2012-02-14 17:02:08

+0

@ G.Bach謝謝你闡述我的答案。你有我的贊成票。 – jpm 2012-02-14 21:12:10

+0

@CharlieMorrison:這個工作的原因是,現在,MPUComp mc只在程序啓動時實例化一次,所以現在CheckMPU(..)的所有調用都引用同一個對象。 – 2012-02-16 18:12:21

0

我看不出任何明顯的代碼可能導致它,所以我只是要建議一些一般的提示。

首先想到的是,你正在重新分配oldTags的地方。查看你的代碼,看看你是否在某處設置了oldTags。我注意到你有它作爲一個公共變量。你有這個理由嗎?除非有充分的理由讓他們公開,否則你應該始終使變量保持私密。

之後,開始尋找方法調用,從ArrayLists中刪除元素(清除,刪除,設置)。

1

它看起來像每次您的動作偵聽器之一被觸發時,它會創建一個新的mpuChecker,並且它們中的每一個創建它自己的MPUComp(與原始無關)。正是這些不相關的MPUComp其中refreshupdateTextArea被稱爲,而不是MPUComp這一切都從此開始。因此,這些新MPUComp旨意無法訪問任何數據在原來的(包括ArrayList的內容。

+0

我該如何解決它。我認爲這可能是一個問題,但我不知道在哪裏做mC = new mpuChecker();以便這兩個動作偵聽器都可以訪問 – 2012-02-14 01:52:44