Java 将单元格值添加到JTable时发生ArrayIndexOutOfBoundException
如上图截图所示 当用户选择项目和项目名称并单击“添加”按钮时,会出现以下情况:Java 将单元格值添加到JTable时发生ArrayIndexOutOfBoundException,java,swing,jtable,indexoutofboundsexception,Java,Swing,Jtable,Indexoutofboundsexception,如上图截图所示 当用户选择项目和项目名称并单击“添加”按钮时,会出现以下情况: 从MySQL数据库检索所选项目、项目名称和成本 显示输入对话框以从用户处获取数量 单击“确定”进入“数量输入”对话框后,显示“输入”对话框以获得折扣 单击“确定”进入“折扣输入”对话框后,将计算总额并显示在“总额”列中 但随后显示ArrayIndexOutOfBound错误,如下所示: java.lang.ArrayIndexOutOfBoundException: 1>=1 java.lang.ArrayI
ArrayIndexOutOfBound
错误,如下所示:
java.lang.ArrayIndexOutOfBoundException: 1>=1
java.lang.ArrayIndexOutOfBoundException: 2>=2
然后,在选择另一个项目和项目名称并执行上述过程后,显示与以下相同的错误:
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();
}
}
删除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
表示只有一行,但您正在访问表示第二行的第一个索引行。但没有第二行。您添加的新行为true,但当您调用getValueAt()
时,单元格应该存在
在我的代码中
int i = jTable1.getRowCount()-1;
int i将获取最后一行索引,例如,如果只有1行,则i为0。因此,要从当前行获取值,请使用
getValueAt(i,2)
。与setValueAt()相同代码>您的表中只有2行。但您正试图访问100行。这是为什么?这是为了什么(i=0;ithat for loop总是会给您提供ArrayIndexOutOfBoundsException
@tenten error1>=1
表示只有一行,但您正在访问第一个索引行,表示第二行。但没有第二行。您添加了新的行,这是真的,但当您调用getValueAt()时
单元格应该存在。您可能希望将这些循环更改为:for(j=0;j
。顺便问一下,为什么要在两个嵌套的for循环中执行这些工作?FastSnail在我之前就做过:-)我这样更改了double icost=(double)tableSale.getModel().getValueAt(I,2);
它可以工作,谢谢:)@快蜗牛