ArrayIndexOutOfBoundsException when populating sql results to Jtable

76 views Asked by At

When I populate SQL results to Jtable, I use arrays instead of vectors. But I got the famous error: Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException when I want to access the JTable (tm.getValueAt(i,12)) data and I do not know what is causing this error:

public  JTable display () throws ClassNotFoundException, SQLException, ParseException{ 
        java.sql.Connection sqlConnection = getSQLConnection();
        Statement stmt = sqlConnection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT test.CHAINE,plg.Séquence,test.FAMILLE,test.REF,test.OFINTERNE,"
                + "test.CMDCLIENT,test.QTE,test.QTEBAC,test.effectif,plg.RENDEMENT,test.TPSn,"
                + "plg.THA,plg.RendPerte,plg.DateDébut,plg.Délai,plg.DateFin "
                + "from planning plg full outer join TEST_LAMIAA test on test.OFINTERNE=plg.NOF "
                + "where test.termine IS NULL "
                + "ORDER BY  test.chaine,CASE WHEN plg.Séquence Is NULL Then 1 Else 0 End, plg.Séquence");

        DefaultTableModel dtm = new DefaultTableModel() {
        @Override
         public Class<?> getColumnClass(int col) {
       if (col == 1) {
        return Integer.class;
    } else {
        //return getValueAt(0, columnIndex).getClass();
        return String.class;
    }

    }
       public boolean isCellEditable(int rowIndex, int columnIndex) {
        //ici la cellule (1, 2) est non-editable
        if (columnIndex == 0 || columnIndex == 5 || columnIndex == 2 || columnIndex == 15
                || columnIndex == 3 || columnIndex ==10 || columnIndex == 4 || columnIndex == 11
                || columnIndex == 13 || columnIndex == 11) 
            return false;
        //le reste est editable
        return true;
    }

};
       ResultSetMetaData rsmetaData = rs.getMetaData();
        //stores the number of columns
        int colmns = rsmetaData.getColumnCount();
        // the object that will pass data to the jTable 

            while (rs.next())
            {
                Object [] rowData = new Object[colmns];
                for (int i = 0; i < colmns;i++)
                {
                    if (rs.getObject(i+1) != null){
                    rowData[i] = rs.getObject(i+1);
                    }
                }
                dtm.addRow(rowData);
            }
         jTable1.setModel(dtm);

        return jTable1;
   }

public JTable modifyTable (JTable jTable) throws ClassNotFoundException, SQLException, ParseException{

  Object Perte = tm.getValueAt(i,12);
}
1

There are 1 answers

1
kingfrito_5005 On

Its possible that you are getting the error in your if statement at 'rs.getObject(i+1).' In this case, even though you are checking it for null, you are still trying to perform that check by accessing data outside the range of the array (In order to checkfor null, you must first access the data.) I do not know for sure whether or not Javascript handles this automatically, as some languages do but other do not. If it does not then that is likely your problem. A try/catch would solve this if that is the case.