我有一个组合框,使用附加了监听器的模型来捕获所选项目:
deleteCombo = new JComboBox<Course>(course.toArray(new Course[course.size()]));
model = new DefaultComboBoxModel<Course>( course.toArray(new Course[course.size()]) );
deleteCombo.setModel( model );
deleteCombo.setRenderer(renderer);
deleteCombo.setSelectedIndex(0);
deleteCombo.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JComboBox deleteCombo = (JComboBox)e.getSource();
Course course = (Course)deleteCombo.getSelectedItem();
deleteNumTxt.setText( course.getCourseNum() ); //This is line 391
deleteTitleTxt.setText( course.getCourseTitle() );
deleteDurationTxt.setText( String.valueOf(course.getDuration() ) );
deleteFeeTxt.setText("$" + String.valueOf(course.getCourseFee() ) );
deleteLevelTxt.setText( course.getCourseLevel() );
}
});
我正在使用itemSelected事件填充GUI中的文本字段以显示所选课程的详细信息。选择课程并单击“删除课程”按钮后,将转到此方法:
public static void deleteCourseRecord() {
courseNumIn = deleteNumTxt.getText();
int index = course.indexOf(new Course(courseNumIn));
course.remove(index);
//RE-POPULATE EDIT AND DELETE COMBOBOXES
model.removeAllElements();
for(Course item : course) {
model.addElement(item);
}
}
记录被删除但会抛出此异常:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MyCourse$2.actionPerformed(MyCourse.java:391)
at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1260)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:1331)
at javax.swing.JComboBox.intervalRemoved(JComboBox.java:1351)
at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:179)
at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:175)
at MyCourse$Course.deleteCourseRecord(MyCourse.java:1138)
at MyCourse.actionPerformed(MyCourse.java:883)
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)
tjavax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
我认为构建actionPerformed部分的方式存在问题,但我不知道它是什么。如果我没有使用模型(或任何其他方法)刷新组合框,它不会抛出异常。如果我注释掉第391行(在代码中标记),它将在第392行抛出异常,依此类推。如果填充文本字段的所有代码行都被注释掉,则它不会抛出任何异常,但当然,函数会丢失。关于我做错了什么的任何想法?干杯
答案 0 :(得分:2)
问题如下:
每次更改设置为组合框的模型时,都会调用动作侦听器。
所以当你这样做时:
//RE-POPULATE EDIT AND DELETE COMBOBOXES
model.removeAllElements();
执行actionlistener并
Course course = (Course)deleteCombo.getSelectedItem();
为null,因为您刚从组合框中删除了所有元素。
如果你尝试这个简单的例子,你可以看到这个:
public static void main(String[] args)
String [] s = {"1", "2"};
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(s);
JComboBox<String> j = new JComboBox<String>(s);
j.setModel(model);
j.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println(((JComboBox) arg0.getSource()).getSelectedItem());
}
});
JFrame jf = new JFrame();
jf.add(j);
jf.setVisible(true);
jf.setSize(100, 100);
model.removeAllElements();
}
它将输出null
。