请协助我的 JTable 循环异常

PLease assist with my JTable Loop Exception

这是我的代码,我试图在其中遍历我的新股票 table 中的所有行。即使当我使用 totalRows - 1 时,我仍然得到相同的异常。任何帮助将不胜感激。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
int totalRows = newTable.getRowCount();
String tempName = "";
int tempTotal = 0;
System.out.println("total new rows: " + totalRows);

    try {
        for (int i = 0; i < (totalRows - 1); i++) {
            tempName = currentTable.getModel().getValueAt(i, 0).toString();
            tempTotal = Integer.parseInt(currentTable.getModel().getValueAt(i, 1).toString());
            System.out.println("Current Table Product:    " + currentTable.getModel().getValueAt(i, 0).toString());
            System.out.println("Current Table Total:  " + currentTable.getModel().getValueAt(i, 1).toString());

            System.out.println("New Table Product:    " + newTable.getModel().getValueAt(i, 0).toString());
            System.out.println("New Table Temp Total:  " + newTable.getModel().getValueAt(i, 1).toString());

            //if (newTable.getModel().getValueAt(i, 0).toString() == currentTable.getModel().getValueAt(i, 0).toString()) {
            //    int newTempTotal = Integer.parseInt(newTable.getModel().getValueAt(i, 1).toString());
            //    newTotal = newTempTotal + tempTotal;
            //    System.out.println("product:    " + newTable.getModel().getValueAt(i, 0).toString());
            //    System.out.println("grand new total:    " + newTotal);
            //}
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}    

然后我得到以下异常...

java.lang.ArrayIndexOutOfBoundsException: 3 >= 3
at java.util.Vector.elementAt(Vector.java:477)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648)
at great.meat.AddNewStock.jButton1ActionPerformed(AddNewStock.java:325)
at great.meat.AddNewStock.access0(AddNewStock.java:23)
at great.meat.AddNewStock.actionPerformed(AddNewStock.java:210)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)

我不知道这两个表有多少行,但您的问题可能出在这一行:

tempName = currentTable.getModel().getValueAt(i, 0).toString();

因为您正在循环使用 newTable 行数:

int totalRows = newTable.getRowCount();

但您正在尝试从 currentTable 获取数据。如果 currentTable 的行数少于 newTable 则它将抛出异常。