1

At the moment im trying to create a GUI interface that can add and store departments via an arrayList and then later reference the department arrayList on an add lecturer form (comboBox) so the lecturer can be assigned to a department. the departments arrayList works for actually adding the departments to the arrayList. But i cannot reference the department arrayList onto my add lecturer form(comboBox) an error appears "null pointer exception" (screenshot below) i have no idea what this error is or how to solve it!

please note the arrayList has been declared in the add lecturer form with the following syntax public ArrayList departments;.

Code is below apologies if i haven't been detailed enough.

submitDepartmentButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        department new_dept= new department(DepartmentNameText.getText(), 
            UserNameFeild.getText(), passwordLabel.getText());

        departments.add(new_dept);                            

        System.out.println(new_dept);

        JComboBox SelectDepartmentTypeComboBox = new JComboBox ();
        SelectDepartmentTypeComboBox.setBounds(211, 254, 187, 31);      

        for (int index = 0; index < departments.size(); index++) {
            SelectDepartmentTypeComboBox.addItem(departments.get(0)); 
            System.out.println("end");
        }
    }
}

I expect the add lecturer form to be able to loop through the department arrayList onto the comboBox but instead a NULL POINTER EXCEPTION appears

Mr. Wrong
  • 500
  • 7
  • 21
J.Moz
  • 11
  • 3
  • Can you share the error more accurately? Which line is failing? – Matias Fuentes Apr 09 '19 at 13:17
  • Why this `JComboBox SelectDepartmentTypeComboBox = new JComboBox ();` is commented out? Its the initialization of the targeted combobox. right? – Master Po Apr 09 '19 at 13:19
  • hi the error appears at the third line of the for loop Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at lecturerAddForm.initialize(lecturerAddForm.java:153) at lecturerAddForm.(lecturerAddForm.java:55) at homeGraphical$5.actionPerformed(homeGraphical.java:141) – J.Moz Apr 09 '19 at 13:22
  • hi the comboBox is not commented out just an error i made when posting in the code – J.Moz Apr 09 '19 at 13:23

0 Answers0