PLease assist with my JTable Loop Exception

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

PLease assist with my JTable Loop Exception

问题

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

  1. 这是我的代码我正在尝试循环遍历我的新库存表中的所有行即使我使用totalRows - 1我仍然收到相同的异常任何帮助将不胜感激
  2. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  3. int totalRows = newTable.getRowCount();
  4. String tempName = "";
  5. int tempTotal = 0;
  6. System.out.println("总新行数:" + totalRows);
  7. try {
  8. for (int i = 0; i < (totalRows - 1); i++) {
  9. tempName = currentTable.getModel().getValueAt(i, 0).toString();
  10. tempTotal = Integer.parseInt(currentTable.getModel().getValueAt(i, 1).toString());
  11. System.out.println("当前表产品:" + currentTable.getModel().getValueAt(i, 0).toString());
  12. System.out.println("当前表总计:" + currentTable.getModel().getValueAt(i, 1).toString());
  13. System.out.println("新表产品:" + newTable.getModel().getValueAt(i, 0).toString());
  14. System.out.println("新表临时总计:" + newTable.getModel().getValueAt(i, 1).toString());
  15. //if (newTable.getModel().getValueAt(i, 0).toString() == currentTable.getModel().getValueAt(i, 0).toString()) {
  16. // int newTempTotal = Integer.parseInt(newTable.getModel().getValueAt(i, 1).toString());
  17. // newTotal = newTempTotal + tempTotal;
  18. // System.out.println("产品:" + newTable.getModel().getValueAt(i, 0).toString());
  19. // System.out.println("新总计:" + newTotal);
  20. //}
  21. }
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. }
  25. }

然后,我收到以下异常:

  1. java.lang.ArrayIndexOutOfBoundsException: 3 >= 3
  2. at java.util.Vector.elementAt(Vector.java:477)
  3. at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648)
  4. at great.meat.AddNewStock.jButton1ActionPerformed(AddNewStock.java:325)
  5. at great.meat.AddNewStock.access$300(AddNewStock.java:23)
  6. at great.meat.AddNewStock$4.actionPerformed(AddNewStock.java:210)
  7. 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.

  1. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  2. int totalRows = newTable.getRowCount();
  3. String tempName = &quot;&quot;;
  4. int tempTotal = 0;
  5. System.out.println(&quot;total new rows: &quot; + totalRows);
  6. try {
  7. for (int i = 0; i &lt; (totalRows - 1); i++) {
  8. tempName = currentTable.getModel().getValueAt(i, 0).toString();
  9. tempTotal = Integer.parseInt(currentTable.getModel().getValueAt(i, 1).toString());
  10. System.out.println(&quot;Current Table Product: &quot; + currentTable.getModel().getValueAt(i, 0).toString());
  11. System.out.println(&quot;Current Table Total: &quot; + currentTable.getModel().getValueAt(i, 1).toString());
  12. System.out.println(&quot;New Table Product: &quot; + newTable.getModel().getValueAt(i, 0).toString());
  13. System.out.println(&quot;New Table Temp Total: &quot; + newTable.getModel().getValueAt(i, 1).toString());
  14. //if (newTable.getModel().getValueAt(i, 0).toString() == currentTable.getModel().getValueAt(i, 0).toString()) {
  15. // int newTempTotal = Integer.parseInt(newTable.getModel().getValueAt(i, 1).toString());
  16. // newTotal = newTempTotal + tempTotal;
  17. // System.out.println(&quot;product: &quot; + newTable.getModel().getValueAt(i, 0).toString());
  18. // System.out.println(&quot;grand new total: &quot; + newTotal);
  19. //}
  20. }
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }

Then I get the following exception...

  1. java.lang.ArrayIndexOutOfBoundsException: 3 &gt;= 3
  2. at java.util.Vector.elementAt(Vector.java:477)
  3. at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648)
  4. at great.meat.AddNewStock.jButton1ActionPerformed(AddNewStock.java:325)
  5. at great.meat.AddNewStock.access$300(AddNewStock.java:23)
  6. at great.meat.AddNewStock$4.actionPerformed(AddNewStock.java:210)
  7. 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:

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

because you are looping using the newTable row count:

  1. 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:

确定