Exception Adding elements to a JList

I'm writing code to retrieve data from a MySQL database. I want to add one column of the query to a JList.

This is my code:

    try {
        ArrayList<String> names = new ArrayList();
        db = new Database();
        db.connect();
        Statement st = db.getConnection().createStatement();
        ResultSet rs = st.executeQuery("SELECT nombre FROM alumnos");

        while (rs.next()) {
            names.add(rs.getString("nombre"));
        }

        DefaultListModel listModel = new DefaultListModel();
        for(String nms : names) {
            listModel.addElement(nms);
        }

        //This is the line 140
        // and lstInicio is the JList
        lstInicio.setModel(listModel);

    } catch (SQLException sql) {
        System.err.println(sql.getMessage());
    }

I think everything is ok, but when I reach this part of the code I get an Exception:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at proyecto.ausentismo.MainWindow.loadList(MainWindow.java:140)
at proyecto.ausentismo.MainWindow.<init>(MainWindow.java:21)
at proyecto.ausentismo.Login.btnLoginActionPerformed(Login.java:121)
at proyecto.ausentismo.Login.access$000(Login.java:11)
at proyecto.ausentismo.Login$1.actionPerformed(Login.java:49)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener$Actions.actionPerformed(BasicButtonListener.java:303)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1664)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2879)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2926)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2842)
at java.awt.Component.processEvent(Component.java:6282)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
at java.awt.Component.dispatchEventImpl(Component.java:4731)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Does anybody know what is causing this exception? How could I find out? EDIT: I commented the line of the error. And stil get the Exception

Answers


As I suspected, your lstInicio variable is null. The solution: look back into your code to find out where you think that you initialized it, and to see why it didn't actually get initialized. That's about all we can say based on what you've posted so far.


Edit: @jesuscc29: glad you've got it working. The key lesson here is not so much the answer to the specific problem that you're having, but rather the general process of debugging a NullPointerException (NPE), since I can guarantee that you'll run into these buggers time and time again. Always first find out which line, and then which variable on that line is causing the NPE to occur, and then try to trace back into your code to see why the variable is null. Do this, and 90% of the time you'll find the problem and it's solution.


Need Your Help

django auto_add_now not working

python django

I have a model in which error is thrown when ever i use python manage.py syncdb

Matching SQL columns and parsing them into another SQL table

php mysql sql

I have an SQL table that contains a column named 'Make' and one named 'Model'. Each row has its own 'Make' and 'Model'.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.