英文:
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 = "";
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();
}
}
Then I get the following exception...
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)
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论