Arrayindexoutofboundsexception when trying to fill a Jtable from a query

Asked

Viewed 45 times

0

I am performing 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();

    }

1 answer

0


Your code is not reproducible so you can’t be sure if it’s the only problem with it, but the column array only has one index and you seem to be trying to configure 30 columns. Obviously it won’t work, since you passed the variable colunas as a parameter of TableModel, And in this variable is a sequence of strings being concatenated which, I imagine, should be the columns. But by concatenating everything, you’ve made it a single string occupying only one index and not an array with 30 different.

You need to separate each value between double quotes and separate with comma, not concatenate with + how are you 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"};
  • That’s right... I didn’t even notice many hours looking at the same code..., Thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.