tags:

views:

330

answers:

2

Hi,

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)
A: 

Relatively simple solution, but since you've only accepted 1 answer out of the last 15 questions you have asked I don't see the point in answering the question. Either you don't read the answers or you don't appreciate the help you receive.

camickr
This shouldn't be answer. It should be a comment.
Asaph
I've added it as a comment other postings, but usually after others have already posted an answer. I figured adding it as the first answer would make it stand out better and the poster couldn't miss the comment. This way we know if Johanna doesn't revist old postings and start accepting them, then my assumptions are true.
camickr
I now have 4 people who voted this answer down. I'm still waiting for the poster to prove me wrong and actually accept the answer that was given. It amazes me I didn't get more "up votes" for warning people about the posters "bad habits".
camickr
+1 from me -- I'm 100% with you on Johanna.
delfuego
+1  A: 

The problem you have is because you add something in a list

d.add(name);

you clear the list,

d.clear();

then you try to access the element from that list:

String s = d.get(j); // IndexOutOfBoundsException: Index: 0, Size: 0
True Soft
Did you read my comment? People won't change if you continue to spoon feeed them answers. All the questions posted by this individual are basic homework questions and the individual has not listened to suggestions on simple ways to debug code or on following up with a simple "thank you", which is done by accepting the proper answer. Only when people stop providing answers will the behaviour change because then the individual will start failing the course.
camickr