简体   繁体   English

将单元格值添加到JTable时的ArrayIndexOutOfBoundException

[英]ArrayIndexOutOfBoundException when adding cell values to a JTable

在此输入图像描述

As above screenshot; 如上图所示;

When user selects Item and Item Name and click Add button, below scenarios happen: 当用户选择项目和项目名称并单击添加按钮时,会出现以下情况:

  1. The selected Item,Item Name and cost retrieves from the MySQL database. 所选项目,项目名称和成本从MySQL数据库中检索。
  2. Show Input Dialog to get qty from the user. 显示输入对话框以从用户获取数量。
  3. After clicking OK to qty Input Dialog, Show Input Dialog to get Discount. 单击确定以输入数量输入对话框后,显示输入对话框以获得折扣。
  4. After clicking OK to discount Input Dialog,the total is calculated and shows in the total Column. 单击“确定”以对“输入对话框”进行折扣后,将计算总数并显示在“总列”中。

But then shows the ArrayIndexOutOfBound error as: 但随后将ArrayIndexOutOfBound错误显示为:

java.lang.ArrayIndexOutOfBoundException: 1>=1

Then after selecting another Item and Item Name and doing the above process shows same error as: 然后选择另一个项目和项目名称并执行上述过程显示相同的错误:

java.lang.ArrayIndexOutOfBoundException: 2>=2 

Also reset Qty, Discount, Total columns to be equal as the last row values. 同时将Qty,Discount,Total列重置为最后一行值。

Add button action performed method; 添加按钮动作执行方法;

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();
    }
}  

This is the Stack Trace; 这是堆栈跟踪;

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)

remove for loop and use this code 删除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 mean there is only one row but you are accessing 1st index row that mean 2nd row .but there is no a 2nd row.you add new rows that's true but when you call getValueAt() the cell should exist java.lang.ArrayIndexOutOfBoundsException: 1 >= 1表示只有一行,但是你正在访问第一行的第一行索引行。但是没有第二行。你添加新的行是真的,但当你调用getValueAt()细胞应该存在

in my code 在我的代码中

int i = jTable1.getRowCount()-1; 

int i will get the last row index for example if there is only 1 row then i is 0 .so to get value from current row use getValueAt(i, 2); int我将得到最后一行索引,例如,如果只有1行,那么我是0。所以从当前行获取值使用getValueAt(i, 2); .same to setValueAt(); .same to setValueAt();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM