java - 异常似乎终止了 JList 的更新

标签 java swing exception jcombobox

每当选择一个项目时,我都会使用 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)

最佳答案

我会做两件事来使其更加稳健。

  1. 确保 getFileListFromDirectory 永远不会返回 null。如果没有项目,则返回 Collections.emptyList 而不是 null。如果这是不可能的,请在使用“foreach”迭代之前专门检查返回值。此时,空指针将阻止模型更新。 (因此单击空驱动器不会清除文件列表。)
  2. 将 cb.getSelectedItem() 分配给局部变量并在调用 updateLocalFileList 之前检查是否为 null。如果cb.getSelectedItem()为null,您可以选择清除文件列表。

关于java - 异常似乎终止了 JList 的更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3115268/

相关文章:

java - 为什么会引发 'deleted entity passed to persist' -异常?

java - 学习多态性

java - 使用箭头键

javascript - 在 JavaScript 中捕获特定异常

c# - 检查私有(private)字段与捕获异常

java - 如何使用 Hibernate 尽可能快地插入数据

java - 该行返回什么?

java - Hibernate中子实体中排除基本实体的ID属性

java - 我如何在我的 JTextArea 中设置行号?

DateTime 对象的 PHP 异常处理