0

I am trying to populate a JComboBox from my database like so:

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);

And I'm getting the Departement.employees like so :

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;               
}

and this is the query i'm using:

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

I am getting the following error:

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 is the line that says if ( dao.fetchEmp(departement) )

I have already checked this thread and I know what line is returning null but I don't understand why

mari
  • 325
  • 1
  • 5
  • 16
  • I already checked that thread but I still can't figure out where the null pointer is coming from – mari Jun 11 '17 at 19:10
  • I am almost certain looking at your question that `dao` is null. Figure why that object is null. – SomeDude Jun 11 '17 at 19:27

0 Answers0