gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:40:44 26 4
gpt4 key购买 nike

jComboBox.removeAllItems 在添加项目之前不会提示(在从配置文件填充 JComboBox 之前调用一次)。单步执行此代码,我看到 jComboBox.removeAllItems 调用 JComboBox.removeAllItems,后者调用 DefaultComboBoxModel.removeAllElements,后者调用 fireIntervalRemoved。 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)

最佳答案

这不是抛出 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 行。

关于java - 为什么 JComboBox.removeAllItems() 会抛出 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3729256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com