Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Odpowiedz Nowy wątek
2018-11-27 10:03
Łączenie JTextField z MySQL
0

Piszę swoją pierwszą apkę z użyciem MySQL. Ze względu na to, że zawiera dużo okienek, wspomagam się graficznym kreatorem netbeansa do generowania tychże. Zrobiłem już z powodzeniem panele logowania i tym podobne, teraz chcę wypisać dane wszystkich administratorów w polu JTextField aby można było je później wybrać i przejść do edycji kont. Przy odpalaniu tego okienka, otrzymuję:

run:
Połączenie poprawne!
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at zarzadzanieKontamiAdministratorow$2.getSize(zarzadzanieKontamiAdministratorow.java:104)
    at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1342)
    at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1311)
    at javax.swing.plaf.basic.BasicListUI.getPreferredSize(BasicListUI.java:578)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)
    at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:791)
    at java.awt.Container.layout(Container.java:1512)
    at java.awt.Container.doLayout(Container.java:1501)
    at java.awt.Container.validateTree(Container.java:1697)
    at java.awt.Container.validateTree(Container.java:1706)
    at java.awt.Container.validateTree(Container.java:1706)
    at java.awt.Container.validateTree(Container.java:1706)
    at java.awt.Container.validateTree(Container.java:1706)
    at java.awt.Container.validate(Container.java:1632)
    at java.awt.Container.validateUnconditionally(Container.java:1669)
    at java.awt.Window.pack(Window.java:818)
    at zarzadzanieKontamiAdministratorow.initComponents(zarzadzanieKontamiAdministratorow.java:169)
    at zarzadzanieKontamiAdministratorow.<init>(zarzadzanieKontamiAdministratorow.java:34)
    at PanelAdministratora.jButton3ActionPerformed(PanelAdministratora.java:154)
    at PanelAdministratora.access$100(PanelAdministratora.java:14)
    at PanelAdministratora$2.actionPerformed(PanelAdministratora.java:71)
    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.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:2238)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2296)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4897)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4475)
    at java.awt.Container.dispatchEventImpl(Container.java:2282)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    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:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at zarzadzanieKontamiAdministratorow$2.getSize(zarzadzanieKontamiAdministratorow.java:104)
    at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1342)
    at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1311)
    at javax.swing.plaf.basic.BasicListUI.getPreferredSize(BasicListUI.java:578)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)
    at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:791)
    at java.awt.Container.layout(Container.java:1512)
    at java.awt.Container.doLayout(Container.java:1501)
    at java.awt.Container.validateTree(Container.java:1697)
    at java.awt.Container.validateTree(Container.java:1706)
    at java.awt.Container.validateTree(Container.java:1706)
    at java.awt.Container.validateTree(Container.java:1706)
    at java.awt.Container.validateTree(Container.java:1706)
    at java.awt.Container.validate(Container.java:1632)
    at java.awt.Window.dispatchEventImpl(Window.java:2744)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    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:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at zarzadzanieKontamiAdministratorow$2.getSize(zarzadzanieKontamiAdministratorow.java:104)
    at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1342)
    at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1311)
    at javax.swing.plaf.basic.BasicListUI.getPreferredSize(BasicListUI.java:578)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)
    at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:791)
    at java.awt.Container.layout(Container.java:1512)
    at java.awt.Container.doLayout(Container.java:1501)
    at java.awt.Container.validateTree(Container.java:1697)
    at java.awt.Container.validateTree(Container.java:1706)
    at java.awt.Container.validateTree(Container.java:1706)
    at java.awt.Container.validateTree(Container.java:1706)
    at java.awt.Container.validateTree(Container.java:1706)
    at java.awt.Container.validate(Container.java:1632)
    at java.awt.Window.dispatchEventImpl(Window.java:2744)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    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:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    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)
BUILD SUCCESSFUL (total time: 7 seconds)
import java.awt.Toolkit;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author 
 */
public class zarzadzanieKontamiAdministratorow extends javax.swing.JFrame {

    /**
     * Creates new form zarzadzanieKontamiAdministratorow
     */

    public String login;
    public String haslo;

    public int szer = Toolkit.getDefaultToolkit().getScreenSize().width;
    public int wys = Toolkit.getDefaultToolkit().getScreenSize().height;

    public zarzadzanieKontamiAdministratorow() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList<>();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Zarządzanie kontami administratorów");
        setLocation(szer/4, wys/4);
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Calibri Light", 0, 24)); // NOI18N
        jLabel1.setText("Zarządzanie kontami administratorów");

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zdjecia/logo.png"))); // NOI18N

        jButton1.setText("Wróć");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jList1.setBackground(new java.awt.Color(0, 0, 0));
        jList1.setBorder(new javax.swing.border.MatteBorder(null));
        jList1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jList1.setForeground(new java.awt.Color(255, 255, 255));
        String tab[] = null;
        String polaczenieURL = "jdbc:mysql://localhost:3306/bazadanych?user=root&password=";
        String query = "SELECT * FROM kontaadminow";

        Connection con = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(polaczenieURL);
            Statement stat = con.createStatement();
            ResultSet result = stat.executeQuery(query);
            System.out.println("Połączenie poprawne!");
            result.last();
            int ile = result.getRow();
            result.first();

            for (int j=0; j > ile; j++)
            {
                tab[j] = j + ". Imię i nazwisko: " + result.getString("imieinazwisko") + " | login: " + result.getString("login");
                result.next();
            }

            con.close();
        }
        catch (Exception ex)
        {
            System.out.println(ex);
        }
        jList1.setModel(new javax.swing.AbstractListModel<String>() {
            public int getSize() { return tab.length; }
            public String getElementAt(int i) { return tab[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zdjecia/add_button.png"))); // NOI18N
        jButton2.setText(" Dodaj nowe konto");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zdjecia/edit_icon.png"))); // NOI18N
        jButton3.setText("Edytuj konto");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(40, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(29, 29, 29)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(97, 97, 97)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton2)
                            .addComponent(jButton3))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        new PanelAdministratora().setVisible(true);
        this.dispose();
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        new dodajKontoAdministratora().setVisible(true);
        this.dispose();
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(zarzadzanieKontamiAdministratorow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(zarzadzanieKontamiAdministratorow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(zarzadzanieKontamiAdministratorow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(zarzadzanieKontamiAdministratorow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new zarzadzanieKontamiAdministratorow().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList<String> jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration                   
}

Nie dostaję błędu, kiedy wyrzucam poniższy blok, więc jak mniemam to w nim tkwi problem

jList1.setModel(new javax.swing.AbstractListModel<String>() {
            public int getSize() { return tab.length; }
            public String getElementAt(int i) { return tab[i]; }
        });

Będę wdzięczny za pomoc w rozwiązaniu problemu :)

Pozostało 580 znaków

2018-11-27 10:27
Łączenie JTextField z MySQL
0

Rozwiązałem już problem korzystając z DefaultListModel, temat można zamknąć :)

Pozostało 580 znaków

Odpowiedz
Liczba odpowiedzi na stronę

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0