为什么JComboBox.removeAllItems()会抛出NullPointerException?

时间:2010-09-16 17:42:51

标签: java nullpointerexception jcombobox

jComboBox.removeAllItems在添加项目之前不会抱怨(在从配置文件填充JComboBox之前调用它一次)。单步执行此代码,我看到jComboBox.removeAllItems调用JComboBox.removeAllItems调用调用fireIntervalRemoved的DefaultComboBoxModel.removeAllElements。 jComboBox中有七个项目,fireIntervalRemoved中的循环在抛出NullPointerException之前执行了大约四次。有什么想法吗?

public class MyClass extends javax.swing.JFrame {
    ...
    private void updateComboBox() {
        try {
            jComboBox.removeAllItems();
            ...
public class JComboBox ...
    ...
    public void removeAllItems() {
        checkMutableComboBoxModel();
        MutableComboBoxModel model = (MutableComboBoxModel)dataModel;
        int size = model.getSize();
        if ( model instanceof DefaultComboBoxModel ) {
            ((DefaultComboBoxModel)model).removeAllElements();
            ...
public class DefaultComboBoxModel ...
    ...
    public void removeAllElements() {
        if ( objects.size() > 0 ) {
            int firstIndex = 0;
            int lastIndex = objects.size() - 1;
            objects.removeAllElements();
        selectedObject = null;
            fireIntervalRemoved(this, firstIndex, lastIndex);
            ...
public abstract class AbstractListModel ...
    ...
    protected void fireIntervalRemoved(Object source, int index0, int index1)
    {
    Object[] listeners = listenerList.getListenerList();
    ListDataEvent e = null;

    for (int i = listeners.length - 2; i >= 0; i -= 2) {
        if (listeners[i] == ListDataListener.class) {
        if (e == null) {
            e = new ListDataEvent(source, 
                ListDataEvent.INTERVAL_REMOVED, index0, index1);
        }
        ((ListDataListener)listeners[i+1]).intervalRemoved(e);
        ...

显示java.lang.NullPointerException

at abc.MyClass.jComboBoxActionPerformed(MyClass.java:211)
at abc.MyClass.access$000(MyClass.java:16)
at abc.MyClass$1.actionPerformed(MyClass.java:110)
at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1240)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:1311)
at javax.swing.JComboBox.intervalRemoved(JComboBox.java:1331)
at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:161)
at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:169)
at javax.swing.JComboBox.removeAllItems(JComboBox.java:751)
at abc.MyClass.updateComboBox(MyClass.java:58)
at abc.MyClass.jMenuItemDevicesDeleteActionPerformed(MyClass.java:231)
at abc.MyClass.access$300(MyClass.java:16)
at abc.MyClass$4.actionPerformed(MyClass.java:154)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1223)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1264)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

1 个答案:

答案 0 :(得分:6)

这不是投掷NPE。你的代码是。

java.lang.NullPointerException
at abc.MyClass.jComboBoxActionPerformed(MyClass.java:211)
at abc.MyClass.access$000(MyClass.java:16)
at abc.MyClass$1.actionPerformed(MyClass.java:110)
...

查看MyClass.java,第211行。