ArrayIndexOutOfBoundsException = 0

时间:2015-02-16 12:44:47

标签: java jtable

  

得到DBTableHeaderFill错误:ArrayIndexOutOfBoundsException = 0

并且无法弄明白,为什么会出现......

col是11因为表有12个标题,因为我们知道计数开始于0

我知道它看起来很原始,但我只是在调试问题...... JOptionPane是为了看看什么不起作用,我无法想象什么是whit数组......

    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);
    }

当我在数组之前分配col和row值时,我得到了这个错误:

  

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

所以我发现了我的问题...

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

这需要在数组的末尾....

2 个答案:

答案 0 :(得分:1)

在使用它们创建数组之后,您似乎正在初始化行/列值:

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

这应该是:

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

col应该是你的表的确切列数,否则你创建的数组只有11个元素,而不是12个。

答案 1 :(得分:0)

变量col设置在:

之后
Object columns [] = new Object[col];

此行之前col的值是多少? 如果它是0,那么这将解释你得到的错误:

  

得到DBTableHeaderFill错误:ArrayIndexOutOfBoundsException = 0