-2

Got DBTableHeaderFill Error: ArrayIndexOutOfBoundsException = 0

and can't figure it out, why does it appears...

col is 11 because table has 12 headers, as we know the counting starts form 0

i know it looks primitive, but i'm just debugging the problem ... JOptionPane is for seeing what works what not, and i can't figure whats the problem whit arrays...

    Object rows[][] = new Object[row][col];
    Object columns [] = new Object[col];
    row = 1;
    col = 11;
    JTable table = new JTable(rows, columns);
    scrollPane.setViewportView(table);
    /*
     *  DataBase Headers
     */
    try{
        conn = DBConnection.DBConnector();
        JOptionPane.showMessageDialog(null, "Connection");
        Statement st = conn.createStatement();
        JOptionPane.showMessageDialog(null, "Statement");
        rs = st.executeQuery("SELECT * FROM Query1");
        JOptionPane.showMessageDialog(null, "Query1 load");
        ResultSetMetaData rsmd = rs.getMetaData();
        JOptionPane.showMessageDialog(null, rsmd);
        int columnCount = rsmd.getColumnCount();
        JOptionPane.showMessageDialog(null, columnCount);
        JOptionPane.showMessageDialog(null, "test " + col);
        // The column count starts from 0
        for (int i = 1; i < columnCount + 1; i++ ) {
            columns[i-1] = rsmd.getColumnName(i);
            JOptionPane.showMessageDialog(null, "result: " + o);
            JOptionPane.showMessageDialog(null, "Header" + columns[i-1]);
          // Writes headers
        }
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, "DBTableHeaderFill Error: " + e);
    }

When i assign col and row values before arrays i get this errors:

java.lang.NullPointerException at javax.swing.JTable$1.getColumnName(Unknown Source) at javax.swing.JTable.addColumn(Unknown Source) at javax.swing.JTable.createDefaultColumnsFromModel(Unknown Source) at javax.swing.JTable.tableChanged(Unknown Source) at javax.swing.JTable.setModel(Unknown Source) at javax.swing.JTable.(Unknown Source) at javax.swing.JTable.(Unknown Source) at javax.swing.JTable.(Unknown Source) at Main.initialize(Main.java:199) at Main.(Main.java:65) at Main$1.run(Main.java:34) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$300(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

So i found my problem...

JTable table = new JTable(rows, columns);
scrollPane.setViewportView(table);

this needs to be at the end of the arrays....

Mr.Pengu
  • 107
  • 1
  • 2
  • 10

2 Answers2

1

It seems you are initialising your row/col values after using them to create the arrays:

Object rows[][] = new Object[row][col];
Object columns [] = new Object[col];
row = 1;
col = 11;

This should probably be:

row = 1;
col = 11;
Object rows[][] = new Object[row][col];
Object columns [] = new Object[col];

And col should be the exact number of columns your table has, otherwise you are creating an array that's only 11 elements long, instead of 12.

tringel
  • 395
  • 1
  • 13
  • i know, but when i assign the int values before the arrays the whole thing just crashess – Mr.Pengu Feb 16 '15 at 12:54
  • Then you should find out what causes these crashes. Creating an array with zero values will lead to an array with no elements, hence you can't even address array[0] as is your case. `String[] strings = new String[0]; System.out.println(strings[0]);` Leads to just the **ArrayIndexOutOfBoundsException: 0** that you are experiencing – tringel Feb 16 '15 at 13:02
  • Thanks, some how forgot about that... got 2 projects going on... and the JTable elements by my look is one of the hardest... – Mr.Pengu Feb 16 '15 at 13:07
0

the variable col is set after:

Object columns [] = new Object[col];

What is the value of col, before this line? If it is 0, then that would explain the error you are getting:

Got DBTableHeaderFill Error: ArrayIndexOutOfBoundsException = 0

Inactive
  • 16
  • 2