添加项目时出现JComboBox错误

时间:2021-03-24 19:01:17

I try to add some items to a JComboBox and I receive a NullPointerException error. This is a sample of my code :

我尝试将一些项添加到JComboBox,并收到NullPointerException错误。这是我的代码示例:

public adminPanel() {
        fillComboTeacher();
        initComponents();
    }
    public void fillComboTeacher(){
        HashSet<Person> set = cont1.returnTeachers();
        Iterator it = set.iterator();
        try {
            while (it.hasNext()) {
                Person p = (Person) it.next();
                String name = p.getName();
                comboTeacher.addItem(name);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

Person class - name, state (with getters and setters)

人类 - 名称,州(有吸气剂和制定者)

returnTeachers() - returns a HashSet containing Persons with state = "Teacher"

returnTeachers() - 返回包含state =“Teacher”的人的HashSet

I don't understand why I'm receiving this error and why I can't fill the ComboBox, I followed a tutorial step-by-step and something is still not well. Thanks!

我不明白为什么我收到这个错误以及为什么我无法填写ComboBox,我一步一步地按照教程进行操作仍然不太好。谢谢!

1 个解决方案

#1


2  

You are calling fillComboTeacher before calling initComponents.

您在调用initComponents之前调用fillComboTeacher。

Looking at the method's name, I assumed that you initialized comboTeacher in the latter.

看一下方法的名称,我假设你在后者中初始化了comboTeacher。

So at that point (comboTeacher.addItem(name);), comboTeacher is not yet initialized and hence the NullPointerException.

所以在那时(comboTeacher.addItem(name);),comboTeacher尚未初始化,因此也就是Nul​​lPointerException。

To fix this, just swap the two method calls in the constructor to initialize your components before trying to use them.

要解决此问题,只需在构造函数中交换两个方法调用以初始化组件,然后再尝试使用它们。

Few notes:

几点说明:

  • A NullPointerException is often easy to fix. Just look at the stacktrace (it indicates the line where the NPE occurs) and see what can be null and why at this line.

    NullPointerException通常很容易修复。只需看看堆栈跟踪(它表示NPE出现的行),看看什么可以为null,为什么在这一行。

  • Don't use a raw iterator, but a generic one (Iterator<Person> it = set.iterator();), that will avoid you to cast the object returned by a next() call.

    不要使用原始迭代器,而是使用泛型迭代器(Iterator it = set.iterator();),这将避免您转换next()调用返回的对象。

  • Since you are only reading the elements in the set, you can simply use a for-each loop for(Person p : cont1.returnTeachers()) comboTeacher.addItem(p.getName());

    由于您只是读取集合中的元素,因此您只需使用for-each循环(Person p:cont1.returnTeachers())comboTeacher.addItem(p.getName());

#1


2  

You are calling fillComboTeacher before calling initComponents.

您在调用initComponents之前调用fillComboTeacher。

Looking at the method's name, I assumed that you initialized comboTeacher in the latter.

看一下方法的名称,我假设你在后者中初始化了comboTeacher。

So at that point (comboTeacher.addItem(name);), comboTeacher is not yet initialized and hence the NullPointerException.

所以在那时(comboTeacher.addItem(name);),comboTeacher尚未初始化,因此也就是Nul​​lPointerException。

To fix this, just swap the two method calls in the constructor to initialize your components before trying to use them.

要解决此问题,只需在构造函数中交换两个方法调用以初始化组件,然后再尝试使用它们。

Few notes:

几点说明:

  • A NullPointerException is often easy to fix. Just look at the stacktrace (it indicates the line where the NPE occurs) and see what can be null and why at this line.

    NullPointerException通常很容易修复。只需看看堆栈跟踪(它表示NPE出现的行),看看什么可以为null,为什么在这一行。

  • Don't use a raw iterator, but a generic one (Iterator<Person> it = set.iterator();), that will avoid you to cast the object returned by a next() call.

    不要使用原始迭代器,而是使用泛型迭代器(Iterator it = set.iterator();),这将避免您转换next()调用返回的对象。

  • Since you are only reading the elements in the set, you can simply use a for-each loop for(Person p : cont1.returnTeachers()) comboTeacher.addItem(p.getName());

    由于您只是读取集合中的元素,因此您只需使用for-each循环(Person p:cont1.returnTeachers())comboTeacher.addItem(p.getName());