简体   繁体   English

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

[英]Why does JComboBox.removeAllItems() throw NullPointerException?

jComboBox.removeAllItems does not complain until after items are added (it gets called once before the JComboBox is populated from a configuration file). 在添加项目之后,jComboBox.removeAllItems才会抱怨(在从配置文件填充JComboBox之前,它将被调用一次)。 Stepping through this code I see jComboBox.removeAllItems calls JComboBox.removeAllItems which calls DefaultComboBoxModel.removeAllElements which calls fireIntervalRemoved. 逐步执行此代码,我看到jComboBox.removeAllItems调用JComboBox.removeAllItems,后者调用DefaultComboBoxModel.removeAllElements,后者调用fireIntervalRemoved。 There are seven items in jComboBox and the loop in fireIntervalRemoved is executed about four times before throwing the NullPointerException. jComboBox中有七个项目,并且在抛出NullPointerException之前,fireIntervalRemoved中的循环执行了大约四次。 Any ideas? 有任何想法吗?

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

It's not throwing an NPE. 它没有抛出NPE。 Your code is. 您的代码是。

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

Look at MyClass.java, line 211. 查看MyClass.java,第211行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM