PLease assist with my JTable Loop Exception

huangapple go评论91阅读模式
英文:

PLease assist with my JTable Loop Exception

问题

以下是您的代码部分的翻译:

这是我的代码我正在尝试循环遍历我的新库存表中的所有行即使我使用totalRows - 1我仍然收到相同的异常任何帮助将不胜感激

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    int totalRows = newTable.getRowCount();
    String tempName = "";
    int tempTotal = 0;
    System.out.println("总新行数:" + 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("当前表产品:" + currentTable.getModel().getValueAt(i, 0).toString());
            System.out.println("当前表总计:" + currentTable.getModel().getValueAt(i, 1).toString());

            System.out.println("新表产品:" + newTable.getModel().getValueAt(i, 0).toString());
            System.out.println("新表临时总计:" + 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("产品:" + newTable.getModel().getValueAt(i, 0).toString());
            //    System.out.println("新总计:" + 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.access$300(AddNewStock.java:23)
at great.meat.AddNewStock$4.actionPerformed(AddNewStock.java:210)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
英文:

Here is my code where I'm trying to loop through all the rows in my new stock table. Even when I use totalRows - 1 I still get the same exeption. Any help will be appreciated.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
int totalRows = newTable.getRowCount();
String tempName = &quot;&quot;;
int tempTotal = 0;
System.out.println(&quot;total new rows: &quot; + totalRows);
try {
for (int i = 0; i &lt; (totalRows - 1); i++) {
tempName = currentTable.getModel().getValueAt(i, 0).toString();
tempTotal = Integer.parseInt(currentTable.getModel().getValueAt(i, 1).toString());
System.out.println(&quot;Current Table Product:    &quot; + currentTable.getModel().getValueAt(i, 0).toString());
System.out.println(&quot;Current Table Total:  &quot; + currentTable.getModel().getValueAt(i, 1).toString());
System.out.println(&quot;New Table Product:    &quot; + newTable.getModel().getValueAt(i, 0).toString());
System.out.println(&quot;New Table Temp Total:  &quot; + 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(&quot;product:    &quot; + newTable.getModel().getValueAt(i, 0).toString());
//    System.out.println(&quot;grand new total:    &quot; + newTotal);
//}
}
} catch (Exception e) {
e.printStackTrace();
}
}    

Then I get the following exception...

java.lang.ArrayIndexOutOfBoundsException: 3 &gt;= 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.access$300(AddNewStock.java:23)
at great.meat.AddNewStock$4.actionPerformed(AddNewStock.java:210)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)

答案1

得分: 1

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

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

因为你正在使用newTable的行数进行循环:

int totalRows = newTable.getRowCount();

但是你试图从currentTable获取数据。如果currentTable的行数少于newTable,那么它会抛出异常。

英文:

I don't know how many rows both tables have, but your problem could be in this line:

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

because you are looping using the newTable row count:

int totalRows = newTable.getRowCount();

but you're trying to get the data from currentTable. If currentTable has less rows than newTable then it will throw an exception.

huangapple
  • 本文由 发表于 2020年7月30日 18:13:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63170991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定