ArrayIndexOutOfBoundsException when trying to populate a JTable from a query

0

I'm running a query in the database to pass the information to a JTable . However, when performing a query, the following error appears:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
    at java.util.Vector.elementAt(Vector.java:474)
    at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:294)
    at Interfaces.Consulta_Kit.tabelaImprimir(Consulta_Kit.java:305)
    at Interfaces.Consulta_Kit.<init>(Consulta_Kit.java:15)
    at Interfaces.Programação.btnConsultarActionPerformed(Programação.java:772)
    at Interfaces.Programação.access$700(Programação.java:8)
    at Interfaces.Programação$8.actionPerformed(Programação.java:361)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Code:

public void tabelaImprimir(String Sql) {
        ArrayList dados = new ArrayList();
        String[] colunas = new String[]{"FK_COD_IDD, COD_CHAPA, DSC_NOME, DSC_CARGO, DSC_SEXO, DSC_IDADE, COD_CPF, COD_PIS, DSC_TEL,"
            + " DSC_END, DSC_COMP, COD_NUM, DSC_BAIRRO, DSC_CIDADE, DSC_CEP, DSC_UF, COD_CTPS, COD_SERIECTPS, DSC_SITE,"
            + " DSC_DATANSC, DSC_DATAAD, DSC_HORARIO, DSC_BP, DSC_CAT, DSC_GESTOR, DSC_DATADESL, DSC_DATAPG, DSC_DISP, "
            + " DSC_MOTIVO, DSC_OBS, DSC_HOMO"};
        conex.conexao();
        conex.executaSql(Sql);

        try {

            conex.rs.first();
            do {
                dados.add(new Object[]{conex.rs.getString("FK_COD_IDD"), conex.rs.getString("COD_CHAPA"), conex.rs.getString("DSC_NOME"), conex.rs.getString("DSC_CARGO"),
                    conex.rs.getString("DSC_SEXO"), conex.rs.getString("DSC_IDADE"), conex.rs.getString("COD_CPF"), conex.rs.getString("COD_PIS"),
                    conex.rs.getString("DSC_TEL"), conex.rs.getString("DSC_END"), conex.rs.getString("DSC_COMP"), conex.rs.getString("COD_NUM"),
                    conex.rs.getString("DSC_BAIRRO"), conex.rs.getString("DSC_CIDADE"), conex.rs.getString("DSC_CEP"), conex.rs.getString("DSC_UF"),
                    conex.rs.getString("COD_CTPS"), conex.rs.getString("COD_SERIECTPS"), conex.rs.getString("DSC_SITE"), conex.rs.getString("DSC_DATANSC"),
                    conex.rs.getString("DSC_DATAAD"), conex.rs.getString("DSC_HORARIO"), conex.rs.getString("DSC_BP"), conex.rs.getString("DSC_CAT"),
                    conex.rs.getString("DSC_GESTOR"), conex.rs.getString("DSC_DATADESL"), conex.rs.getString("DSC_DATAPG"), conex.rs.getString("DSC_DISP"),
                    conex.rs.getString("DSC_MOTIVO"), conex.rs.getString("DSC_OBS"), conex.rs.getString("DSC_HOMO")});

            } while (conex.rs.next());

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(rootPane, "Erro ao Preencher o ArrayList " + ex);
        }

        ModeloTabelaDados model = new ModeloTabelaDados(dados, colunas);

        jTableImrpimir.setModel(model);

        jTableImrpimir.getColumnModel().getColumn(0).setPreferredWidth(23);
        jTableImrpimir.getColumnModel().getColumn(0).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(1).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(1).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(2).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(2).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(3).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(3).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(4).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(4).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(5).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(5).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(6).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(6).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(7).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(7).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(8).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(8).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(9).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(9).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(10).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(10).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(11).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(11).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(12).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(12).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(13).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(13).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(14).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(14).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(15).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(15).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(16).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(16).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(17).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(17).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(18).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(18).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(19).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(19).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(20).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(20).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(21).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(21).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(22).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(22).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(23).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(23).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(24).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(24).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(25).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(25).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(26).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(26).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(27).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(27).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(28).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(28).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(29).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(29).setResizable(true);

        jTableImrpimir.getColumnModel().getColumn(30).setPreferredWidth(30);
        jTableImrpimir.getColumnModel().getColumn(30).setResizable(true);

        jTableImrpimir.getTableHeader().setReorderingAllowed(false);
        jTableImrpimir.setAutoResizeMode(jTableImrpimir.AUTO_RESIZE_OFF);
        jTableImrpimir.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        conex.desconectar();

    }
    
asked by anonymous 10.03.2018 / 22:20

1 answer

0

Your code is not playable so you can not say for sure if that's the only problem with it, but the column array only has an index and you seem to be trying to set up 30 columns. Obviously it will not work, since it passed the colunas variable as a parameter of TableModel ", and in this variable there is a sequence of strings being concatenated which, I suppose, should be the columns. But by concatenating everything, you made it as a single string occupying only one index and not an array with 30 different ones.

You need to separate each value in double quotation marks and separate with comma, and do not concatenate with + as you are doing:

String[] colunas = new String[]{"FK_COD_IDD", "COD_CHAPA", "DSC_NOME",
    "DSC_CARGO", "DSC_SEXO", "DSC_IDADE", "COD_CPF", "COD_PIS", "DSC_TEL", 
    "DSC_END", "DSC_COMP", "COD_NUM", "DSC_BAIRRO", "DSC_CIDADE", "DSC_CEP", 
    "DSC_UF", "COD_CTPS", "COD_SERIECTPS", "DSC_SITE", "DSC_DATANSC", 
    "DSC_DATAAD", "DSC_HORARIO", "DSC_BP", "DSC_CAT", "DSC_GESTOR", 
    "DSC_DATADESL", "DSC_DATAPG", "DSC_DISP", "DSC_MOTIVO", "DSC_OBS", "DSC_HOMO"};
    
10.03.2018 / 22:34