我正在使用ActionListener以在选择项目时更新JLIST。
jComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
updateLocalFileList( cb.getSelectedItem().toString() );
}
});
它正在为 UI 调用此方法。
public void updateLocalFileList( String path ){
DefaultListModel model = new DefaultListModel();
for (String str : LocalFileSystem.getFileListFromDirectory( path )) {
model.addElement( str );
}
getJList().setModel(model);
}
如果 getFileListFromDirectory() 给出 NullPointerException,比如选择了空 dvd 驱动器的盘符,它似乎会阻止 ActionListener 按预期工作。
我不确定到底发生了什么,但我怀疑将空值传递给模型会导致此问题。
有任何想法吗?
编辑
这是所要求的堆栈跟踪。如您所见,该方法显然会在无法访问的驱动器上触发 NullPointerException。我不知道为什么它会阻止 JList 更新,因为应用程序的其余部分工作正常。
java.lang.NullPointerException
at mine.View.updateLocalFileList(View.java:274)
at mine.View$1.actionPerformed(View.java:262)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)