jComboBox.removeAllItems does not complain until after items are added (it gets called once before the JComboBox is populated from a configuration file). Stepping through this code I see jComboBox.removeAllItems calls JComboBox.removeAllItems which calls DefaultComboBoxModel.removeAllElements which calls fireIntervalRemoved. There are seven items in jComboBox and the loop in fireIntervalRemoved is executed about four times before throwing the NullPointerException. 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
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)