尝试从数据库填充JComboBox时出现NullPointerException

时间:2017-06-11 19:04:57

标签: java

我试图从我的数据库中填充JComboBox,如下所示:

JComboBox<String> empField= new JComboBox<String>();
empField.setForeground(Color.BLACK);
empField.setBounds(174, 319, 199, 20);
int departement = User.departementId;
if ( dao.fetchEmp(departement) ) {
    for(User u : Departement.employees) {
        empField.addItem(u.getUsername());
    }
} else {
    JOptionPane.showMessageDialog(null, "No employees in this departement!");
}
panel.add(empField);

我得到了Departement.employees就是这样:

public boolean fetchEmp (int departement) {
    try {
        fetchEmpOfDep_statement = connection.prepareStatement(SQL_EMPOFDEP);
        fetchEmpOfDep_statement.setInt(1, departement);
        ResultSet res = fetchEmpOfDep_statement.executeQuery();
        ArrayList<User> list = new ArrayList<User>();
        Departement.depId = departement;
        System.out.println(Departement.depId);
        while (res.next()){
            User u = new User(res.getInt(1), res.getString(2), (res.getInt(4) != 0), (res.getInt(5) != 0), res.getInt(6));
            list.add(u);
        }
        Departement.employees = list;
        if (res.next()) {
            return true;
        }
    } catch (Exception e) {
         JOptionPane.showMessageDialog(null, e.getMessage());
    }
    return false;               
}

这是我使用的查询:

SQL_EMPOFDEP = "SELECT * FROM employee WHERE employee.departement=?;";

我收到以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at task.addTask.initialize(addTask.java:213)
    at task.addTask.<init>(addTask.java:39)
    at mainMenu.MainMenu$3.actionPerformed(MainMenu.java:77)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

addTask.java:213是表示if ( dao.fetchEmp(departement) )

的行

我已经检查了this thread,我知道什么行返回null但我不明白为什么

0 个答案:

没有答案