-4

I have a frame that has a table and an exit button. I want to add some data to my table, but this doesn't work correctly: when I open my frame at first, the data is OK, e.g., "Nima,Rahmani,...", then I click the exit button and open my frame again, the table now has "Nima,Rahmani,..." two times, and when I click exit button now, an IndexOutOfBoundsException is thrown.

My frame:

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:
    }
}}

stackTrace:

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)
4

1 回答 1

3

您遇到的问题是因为您在列表中添加了一些内容

d.add(name);

你清除列表,

d.clear();

然后您尝试访问该列表中的元素:

String s = d.get(j); // IndexOutOfBoundsException: Index: 0, Size: 0
于 2009-12-14T07:56:14.093 回答