1

enter image description here

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

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.

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)
7
  • you have only 2 rows in your table .but you are trying to access 100 rows .why is that?for what this for(i=0;i<100;i++){ loop ? Commented Jul 26, 2015 at 13:20
  • 1
    that for loop is always going to give you that ArrayIndexOutOfBoundsException Commented Jul 26, 2015 at 13:25
  • 1
    @tenten error 1 >= 1 mean there is only one row but you are accessing 1st index row mean 2nd row .but there is no 2nd row.you add new rows that's true but when you call getValueAt() the cell should exist Commented Jul 26, 2015 at 13:26
  • 1
    You may want to change those loop to something like: for(j=0; j< model.getRowCount(); j++) . By the way, why are you doing those jobs in two nested for loops? Commented Jul 26, 2015 at 13:31
  • 1
    FastSnail did it before me :-) Commented Jul 26, 2015 at 13:38

1 Answer 1

3

remove for loop and use this code

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

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); .same to setValueAt();

Sign up to request clarification or add additional context in comments.

1 Comment

I changed like this double icost = (double) tableSale.getModel().getValueAt(i , 2); It works Thanks :) @Fast Snail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.