954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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

nsyncpilu
Light Poster
49 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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

nsyncpilu
Light Poster
49 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Thanks for repley , i got it solved

nsyncpilu
Light Poster
49 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: