为什么它抛出一个IndexOutOf边界异常?

Why does it throw an IndexOutOfBoundsException?(为什么它抛出一个IndexOutOf边界异常?)

本文介绍了为什么它抛出一个IndexOutOf边界异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个框架,它有一个表和一个退出按钮。我想将一些数据添加到我的表中,但这不能正常工作:当我第一次打开我的框架时,数据是正常的,例如,"NIMA,Rahmani,...",然后我单击Exit按钮,再次打开我的框架,表现在显示"Nima,Rahmani,..."两次,当我现在单击退出按钮时,抛出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)

推荐答案

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

d.add(name);

清除列表,

d.clear();

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

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

这篇关于为什么它抛出一个IndexOutOf边界异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么它抛出一个IndexOutOf边界异常?

基础教程推荐