将单元格值添加到 JTable 时出现 ArrayIndexOutOfBoundException
ArrayIndexOutOfBoundException when adding cell values to a JTable
如上截图;
当用户选择项目和项目名称并单击添加按钮时,会发生以下情况:
- 从 MySQL 数据库中检索所选项目、项目名称和成本。
- 显示输入对话框以获取用户的数量。
- 在点击确定数量输入对话框后,显示输入对话框以获得折扣。
- 点击确定打折输入对话框后,计算总计并显示在总计列中。
但随后将 ArrayIndexOutOfBound
错误显示为:
java.lang.ArrayIndexOutOfBoundException: 1>=1
然后在选择另一个项目和项目名称并执行上述过程后显示与以下相同的错误:
java.lang.ArrayIndexOutOfBoundException: 2>=2
同时将数量、折扣、总计列重置为与最后一行值相等。
添加按钮动作执行方法;
private void add_btnActionPerformed(java.awt.event.ActionEvent evt) {
int i=0;
int j=0;
String temp = (String) IDcombo.getSelectedItem();
String temp2 = (String) Namecombo.getSelectedItem();
String sql = "select ItemID,ItemName,CostPrice from druginfo where ItemID=?";
try {
pst=conn.prepareStatement(sql);
pst.setString(1, temp);
rs=pst.executeQuery();
addDataToTable(tableSale,DbUtils.resultSetToTableModel(rs));//this method for adding multiple lines in the table
IDcombo.setSelectedItem(null);
Namecombo.setSelectedItem(null);
exptxt.setText(null);
instock.setText(null);
//getting user input for selling qty
String Qty=JOptionPane.showInputDialog("Insert Selling Quantity :");
double sellingqty=Double.parseDouble(Qty);
//getting user input for specific item discount
String discount = JOptionPane.showInputDialog("Insert Item Discount");
double idiscount=Double.parseDouble(discount);
for(j=0;j<100;j++){
double icost =(double) tableSale.getModel().getValueAt(j,2);
System.out.println(icost);
//calculating Gross Total
double grosstotal = (sellingqty*icost)-idiscount;
System.out.println(grosstotal);
for(i=0;i<100;i++){
//setting qty input value to table sale
tableSale.getModel().setValueAt(sellingqty,i, 3);
//setting input value to table sale
tableSale.getModel().setValueAt(idiscount,i, 4);
//setting grosstotal value to table sale
tableSale.getModel().setValueAt(grosstotal,i, 5);
}
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "error "+ex);
ex.printStackTrace();
}
}
这是堆栈跟踪;
java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:664)
at com.bit.project.Newsale.add_btnActionPerformed(Newsale.java:720)
java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:664)
at com.bit.project.Newsale.add_btnActionPerformed(Newsale.java:720)
删除 for 循环并使用此代码
int i = jTable1.getRowCount()-1;
double icost = (double) tableSale.getModel().getValueAt(i, 2);
System.out.println(icost);
//calculating Gross Total
double grosstotal = (sellingqty * icost) - idiscount;
System.out.println(grosstotal);
//setting qty input value to table sale
tableSale.getModel().setValueAt(sellingqty, i, 3);
//setting input value to table sale
tableSale.getModel().setValueAt(idiscount, i, 4);
//setting grosstotal value to table sale
tableSale.getModel().setValueAt(grosstotal, i, 5);
java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
表示只有一行,但您正在访问第一个索引行,这意味着第二行。但是没有第二个 row.you 添加新行,但当您调用 getValueAt()
单元格应该存在
在我的代码中
int i = jTable1.getRowCount()-1;
int 我将获得最后一行索引,例如,如果只有 1 行,则 i 为 0。因此要从当前行获取值,请使用 getValueAt(i, 2);
。与 setValueAt();
[=16 相同=]
如上截图;
当用户选择项目和项目名称并单击添加按钮时,会发生以下情况:
- 从 MySQL 数据库中检索所选项目、项目名称和成本。
- 显示输入对话框以获取用户的数量。
- 在点击确定数量输入对话框后,显示输入对话框以获得折扣。
- 点击确定打折输入对话框后,计算总计并显示在总计列中。
但随后将 ArrayIndexOutOfBound
错误显示为:
java.lang.ArrayIndexOutOfBoundException: 1>=1
然后在选择另一个项目和项目名称并执行上述过程后显示与以下相同的错误:
java.lang.ArrayIndexOutOfBoundException: 2>=2
同时将数量、折扣、总计列重置为与最后一行值相等。
添加按钮动作执行方法;
private void add_btnActionPerformed(java.awt.event.ActionEvent evt) {
int i=0;
int j=0;
String temp = (String) IDcombo.getSelectedItem();
String temp2 = (String) Namecombo.getSelectedItem();
String sql = "select ItemID,ItemName,CostPrice from druginfo where ItemID=?";
try {
pst=conn.prepareStatement(sql);
pst.setString(1, temp);
rs=pst.executeQuery();
addDataToTable(tableSale,DbUtils.resultSetToTableModel(rs));//this method for adding multiple lines in the table
IDcombo.setSelectedItem(null);
Namecombo.setSelectedItem(null);
exptxt.setText(null);
instock.setText(null);
//getting user input for selling qty
String Qty=JOptionPane.showInputDialog("Insert Selling Quantity :");
double sellingqty=Double.parseDouble(Qty);
//getting user input for specific item discount
String discount = JOptionPane.showInputDialog("Insert Item Discount");
double idiscount=Double.parseDouble(discount);
for(j=0;j<100;j++){
double icost =(double) tableSale.getModel().getValueAt(j,2);
System.out.println(icost);
//calculating Gross Total
double grosstotal = (sellingqty*icost)-idiscount;
System.out.println(grosstotal);
for(i=0;i<100;i++){
//setting qty input value to table sale
tableSale.getModel().setValueAt(sellingqty,i, 3);
//setting input value to table sale
tableSale.getModel().setValueAt(idiscount,i, 4);
//setting grosstotal value to table sale
tableSale.getModel().setValueAt(grosstotal,i, 5);
}
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "error "+ex);
ex.printStackTrace();
}
}
这是堆栈跟踪;
java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:664)
at com.bit.project.Newsale.add_btnActionPerformed(Newsale.java:720)
java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:664)
at com.bit.project.Newsale.add_btnActionPerformed(Newsale.java:720)
删除 for 循环并使用此代码
int i = jTable1.getRowCount()-1;
double icost = (double) tableSale.getModel().getValueAt(i, 2);
System.out.println(icost);
//calculating Gross Total
double grosstotal = (sellingqty * icost) - idiscount;
System.out.println(grosstotal);
//setting qty input value to table sale
tableSale.getModel().setValueAt(sellingqty, i, 3);
//setting input value to table sale
tableSale.getModel().setValueAt(idiscount, i, 4);
//setting grosstotal value to table sale
tableSale.getModel().setValueAt(grosstotal, i, 5);
java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
表示只有一行,但您正在访问第一个索引行,这意味着第二行。但是没有第二个 row.you 添加新行,但当您调用 getValueAt()
单元格应该存在
在我的代码中
int i = jTable1.getRowCount()-1;
int 我将获得最后一行索引,例如,如果只有 1 行,则 i 为 0。因此要从当前行获取值,请使用 getValueAt(i, 2);
。与 setValueAt();
[=16 相同=]