Saibal Bhaduri
Saibal Bhaduri

Reputation: 81

java.lang.ArrayIndexOutOfBoundsException: 0 >= 0 in swing

I have inserted a JTable in mu JFrame and trying to add rows from a result set, but it is showing

Exception in thread "AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException: 0 >= 0

the code is given below:

String billSelect_Sql = "select bill.bill_id, transaction.date, bill.product_desc, bill.quantity, bill.rate, transaction.debt, transaction.crdt, transaction.closing_balance from bill, transaction where bill.user_id = transaction.user_id and bill.user_id = '"+uid+"';";
            Statement ST2=conn.createStatement();
            ResultSet RS2 = ST2.executeQuery(billSelect_Sql)

            int rowCount = 0;
            while(RS2.next()){

               billing_tab.getModel().setValueAt(RS2.getString("bill_id"), rowCount, 1);
               billing_tab.getModel().setValueAt(RS2.getString("date"), rowCount, 2);
               billing_tab.getModel().setValueAt(RS2.getString("product_desc"), rowCount, 3);
               billing_tab.getModel().setValueAt(RS2.getInt("quantity"), rowCount, 4);
               billing_tab.getModel().setValueAt(RS2.getDouble("rate"), rowCount, 5);
               billing_tab.getModel().setValueAt(RS2.getDouble("debt"), rowCount, 6);
               billing_tab.getModel().setValueAt(RS2.getDouble("crdt"), rowCount, 7);
               billing_tab.getModel().setValueAt(RS2.getDouble("closing_balance"), rowCount, 8);
               rowCount = rowCount+1;
            }

        }catch(ClassNotFoundException | SQLException | NumberFormatException e){
            JOptionPane.showMessageDialog(null, e);
            System.out.println(e);
        }

billing_tab is the Variable name of my jTable on jframe. Showing error at the point:

billing_tab.getModel().setValueAt(RS2.getString("bill_id"), rowCount, 1);

Did I miss anything to add with this code ?

Upvotes: 0

Views: 1037

Answers (1)

camickr
camickr

Reputation: 324088

First of all variable names (ie. RS2, ST2) should NOT start with an upper case character. Then names should also be more descriptive. Most of your variable names are correct, but not all. Be consistent!!!

trying to add rows from a result set

   billing_tab.getModel().setValueAt(RS2.getString("bill_id"), rowCount, 1);
   billing_tab.getModel().setValueAt(RS2.getString("date"), rowCount, 2);
   billing_tab.getModel().setValueAt(RS2.getString("product_desc"), rowCount, 3);
   billing_tab.getModel().setValueAt(RS2.getInt("quantity"), rowCount, 4);
   billing_tab.getModel().setValueAt(RS2.getDouble("rate"), rowCount, 5);
   billing_tab.getModel().setValueAt(RS2.getDouble("debt"), rowCount, 6);
   billing_tab.getModel().setValueAt(RS2.getDouble("crdt"), rowCount, 7);
   billing_tab.getModel().setValueAt(RS2.getDouble("closing_balance"), rowCount, 8);
   rowCount = rowCount+1;

Your model is empty so there is no data to change. Don't try to add data to the model using the setValueAt(...) method. That method is only for "changing" existing data in the model.

Instead you want to add a new row of data. So your code should be something like:

Vector<Object> row = new Vector<Object>();
row.addElement( RS2.getString("bill_id") );
row.addElement( RS2.getSTring("date") );
...
billing_tab.getModel().addRow( row );

Upvotes: 1

Related Questions