2009-12-14 191 views
-4

我有一個框架,有一個表和退出按鈕。我想添加一些數據到我的表格中,但是這不起作用:當我首先打開我的框架時,數據是好的,例如,「Nima,Rahmani,...」,然後單擊退出按鈕並再次打開框架,桌子上現在有兩次「尼瑪,拉馬尼......」,當我現在點擊退出按鈕時,會拋出一個IndexOutOfBoundsException爲什麼會拋出IndexOutOfBoundsException?

我的框架:

public class DeathList extends javax.swing.JFrame { 

private Admin admin; 
private ArrayList<Death> list; 
DefaultTableModel model; 

/** Creates new form DeathList */ 
public DeathList(Admin admin) { 
    initComponents(); 
    this.admin = admin; 
    Manager.admin = admin; 
    try { 
     Manager.addToDeathListFromMySQL(); 
    } catch (SQLException ex) { 
     Logger.getLogger(DeathList.class.getName()).log(Level.SEVERE, null, ex); 
    } 

    fillTable(); 

} 

private void fillTable() { 
    String[] columNames = {"name", "family", "father's name", "date of birth", 
          "date of death", "date of confirmation", 
          "grave column", "grave row"}; 
    List<Death> death = admin.getDeathList(); 
    if (death.isEmpty()) { 
     JOptionPane.showMessageDialog(this, "Death list is empty! First, add a person.", 
             "Error", JOptionPane.ERROR_MESSAGE); 
    } else { 

     Object[][] data = new Object[death.size()][columNames.length]; 
     for (int i = 0; i < data.length; i++) { 
      Death death1 = death.get(i); 
      data[i][0] = death1.getName(); 
      data[i][1] = death1.getFamily(); 
      data[i][2] = death1.getFatherName(); 
      data[i][3] = death1.getDateOfBirth(); 
      data[i][4] = death1.getDateOfDeath(); 
      data[i][5] = death1.getDateOfConfirmation(); 
      data[i][6] = death1.getGraveColumn(); 
      data[i][7] = death1.getGraveRow(); 

     } 
     model = new DefaultTableModel(data, columNames); 
     jTable1.setModel(model); 

    } 

} 
private void cBActionPerformed(java.awt.event.ActionEvent evt) { 
    List<String> d = new ArrayList<String>(); 
    List<String> d1 = new ArrayList<String>(); 
    List<String> d2 = new ArrayList<String>(); 
    List<String> d3 = new ArrayList<String>(); 

    for (int i = 0; i < jTable1.getRowCount(); i++) { 
     String name = (String) jTable1.getValueAt(i, 0); 
     String name1 = (String) jTable1.getValueAt(i, 1); 
     String name2 = (String) jTable1.getValueAt(i, 2); 
     String name3 = (String) jTable1.getValueAt(i, 3); 
     if (name != null && name1 != null && name2 != null && name3 != null) { 
      d.add(name); 
      d1.add(name1); 
      d2.add(name2); 
      d3.add(name3); 
      d.clear(); 
      d1.clear(); 
      d2.clear(); 
      d3.clear(); 
      Object[][] data1 = new Object[i][4]; 
      for (int j = 0; j < data1.length; j++) { 
       String s = d.get(j); 
       String s1 = d1.get(j); 
       String s2 = d2.get(j); 
       String s3 = d3.get(j); 
       data1[j][0] = s; 
       data1[j][1] = s1; 
       data1[j][2] = s2; 
       data1[j][3] = s3; 
      } 

      model = new DefaultTableModel(data1, 4); 
      jTable1.setModel(model); 
      fillTable(); 

     } else { 
      fillTable(); 
     } 
    } 
    int r = JOptionPane.showConfirmDialog(this, "Are you sure?", "Message", 
              JOptionPane.YES_NO_CANCEL_OPTION); 
    if (r == JOptionPane.YES_OPTION) { 
     this.dispose();// TODO add your handling code here: 
    } 
}} 

堆棧跟蹤:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
    at java.util.ArrayList.RangeCheck(ArrayList.java:547) 
    at java.util.ArrayList.get(ArrayList.java:322) 
    at AdminGUI.DeathList.cBActionPerformed(DeathList.java:192) 
    at AdminGUI.DeathList.access$000(DeathList.java:28) 
    at AdminGUI.DeathList$1.actionPerformed(DeathList.java:122) 
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) 
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) 
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) 
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) 
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) 
    at java.awt.Component.processMouseEvent(Component.java:6038) 
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3265) 
    at java.awt.Component.processEvent(Component.java:5803) 
    at java.awt.Container.processEvent(Container.java:2058) 
    at java.awt.Component.dispatchEventImpl(Component.java:4410) 
    at java.awt.Container.dispatchEventImpl(Container.java:2116) 
    at java.awt.Component.dispatchEvent(Component.java:4240) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322) 
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) 
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916) 
    at java.awt.Container.dispatchEventImpl(Container.java:2102) 
    at java.awt.Window.dispatchEventImpl(Window.java:2429) 
    at java.awt.Component.dispatchEvent(Component.java:4240) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121) 

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 
     at java.util.ArrayList.RangeCheck(ArrayList.java:547) 
     at java.util.ArrayList.get(ArrayList.java:322) 
     at AdminGUI.DeathList.cBActionPerformed(DeathList.java:192) 
     at AdminGUI.DeathList.access$000(DeathList.java:28) 
     at AdminGUI.DeathList$1.actionPerformed(DeathList.java:122) 
     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) 
     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) 
     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) 
     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) 
     at 

javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) 
     at java.awt.Component.processMouseEvent(Component.java:6038) 
     at javax.swing.JComponent.processMouseEvent(JComponent.java:3265) 
     at java.awt.Component.processEvent(Component.java:5803) 
     at java.awt.Container.processEvent(Container.java:2058) 
     at java.awt.Component.dispatchEventImpl(Component.java:4410) 
     at java.awt.Container.dispatchEventImpl(Container.java:2116) 
     at java.awt.Component.dispatchEvent(Component.java:4240) 
     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322) 
     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) 
     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916) 
     at java.awt.Container.dispatchEventImpl(Container.java:2102) 
     at java.awt.Window.dispatchEventImpl(Window.java:2429) 
     at java.awt.Component.dispatchEvent(Component.java:4240) 
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) 
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) 
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) 
     at java.awt.EventDispatchThread.run(EventDispatchThread.java:121) 
+14

相對簡單的解決方案,但因爲你只接受了1個回答出你問我沒有看到這一點,在過去的15個問題回答這個問題。要麼你沒有閱讀答案,或者你不明白你收到的幫助 – camickr 2009-12-14 06:34:48

+0

在這裏使用體面的調試器有什麼錯誤?這可能是學習自己發現的好教訓。在Eclipse中,例如,您可以添加某些要識別的異常(斷點視圖 - >「添加Java異常斷點」),並讓調試器每當*會出現時停止。意思是:讓程序運行並停止在發生異常(IndexOutOfBoundsException)的位置。然後你會使用變量視圖並檢查發生了什麼。 – pimpf0r 2009-12-14 09:25:39

+0

如果我的答案是有用的,你什麼時候將它作爲你接受的答案來檢查? – 2009-12-15 17:35:19

回答

3

你有這個問題,是因爲你在一個列表

d.add(name); 

你清晰的列表中添加東西,

d.clear(); 

然後嘗試從該列表訪問元素:

String s = d.get(j); // IndexOutOfBoundsException: Index: 0, Size: 0 
+5

您是否看過我的評論?如果你繼續向他們提供答案,人們不會改變。這個人發佈的所有問題都是基本的家庭作業問題,個人沒有聽取關於調試代碼的簡單方法的建議,或者通過接受正確答案來完成簡單的「謝謝」。只有當人們停止提供答案時,行爲纔會改變,因爲那時個人會開始失敗。 – camickr 2009-12-14 15:44:17

+0

@camickr相當容易,我近3年後偶然發現這個問題,這個答案是正確的,唯一一個發佈的,仍然不被接受。 – 2012-11-02 15:09:39

相關問題