Hy , i read all the other posts about this error but didn`t helped me , tried google but notheing.

I have the following method :

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
        DefaultTableModel model = (DefaultTableModel)jTable2.getModel();
        int nrrows = jTable2.getSelectedRowCount();
        int[] rows = jTable2.getSelectedRows();
        System.out.println("randuri selectate " +nrrows);
        int j,i;
        int col=0;
        Object value = new Object();
           for(i=0;i<nrrows;i++){
              value = model.getValueAt(rows[i] , col); 
              System.out.print("randul"+rows[i]+" " );
              System.out.print( value);
              System.out.println(" ");
              String connectionURL = "jdbc:mysql://localhost:3306/bazadedate";
              Connection connection;
              try{
                  Class.forName("com.mysql.jdbc.Driver");
                  connection = DriverManager.getConnection(connectionURL, "root", "123456");
                  PreparedStatement pst =  connection.prepareStatement("DELETE FROM tabel WHERE id="+value);
                  j = pst.executeUpdate();
                  model.removeRow(rows[i]);
              }
              catch(Exception e){
                  System.out.println("The exception is " + e);
               }
             
           }         
    }

And i keeps throwing me the same error :

"AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5 >= 4


The task of the button when it`s clicked is to delete from database the records with the id ... .

loop backwards, currently once you do removeRow(0) the former row 1 becomes row 0, then when you doe removeRow(1) (really row 2 now) the original row 3 becomes row 1, etc, etc.

commented: that right +1 +8

Thanks for hint but I tried that and it keeps giveing me the same error.

Did start at [nrrows] or [nrrows-1]?

Put in a println debug statement just before the line the error occurs on and figure out why it's sending too high an index value.

Correct, the number of items in a list is one greater than the last index in the list.

Thanks for repley , i got it solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.