Java 带有ListSelectionListener的奇怪随机NPE

Java 带有ListSelectionListener的奇怪随机NPE,java,swing,nullpointerexception,jlist,listselectionlistener,Java,Swing,Nullpointerexception,Jlist,Listselectionlistener,在我编写的lil应用程序中,我有一个随机出现的NullPointerException。 基本上,它应显示一个数字列表,当选择其中一个时,一些细节应显示在窗口的另一部分 单击按钮添加新数据集时,会打开一个新窗口,其中包含一些文本字段和一个按钮,用于将其发送到数据库,并更新列表以包含网络数据集,以及选择该数据集 在这个位置上,我随机得到一个NPE(见下面的日志)。有趣的是——无论是否有错误,一切都正常。只有窗户没有关上。 当我在调试模式下运行时,它永远不会发生 要查看日志,请执行以下操作: 这是

在我编写的lil应用程序中,我有一个随机出现的NullPointerException。 基本上,它应显示一个数字列表,当选择其中一个时,一些细节应显示在窗口的另一部分

单击按钮添加新数据集时,会打开一个新窗口,其中包含一些文本字段和一个按钮,用于将其发送到数据库,并更新列表以包含网络数据集,以及选择该数据集

在这个位置上,我随机得到一个NPE(见下面的日志)。有趣的是——无论是否有错误,一切都正常。只有窗户没有关上。 当我在调试模式下运行时,它永远不会发生

要查看日志,请执行以下操作:

这是SelectionListener:

menu.itemList.addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        String index = menu.itemList.getSelectedValue().toString(); //mentioned Line 63
        TDMInvDB db = new TDMInvDB();
        try {
            details.descr.setText(db.getDescr(index));
            details.specs.setText(db.getSpecs(index));
            details.historyList.setListData(db.getHistory(index));
            if(Integer.parseInt(index) < 0) {
                details.add.setEnabled(false);
                details.edit.setEnabled(false);
            } else {
                details.add.setEnabled(true);
                details.edit.setEnabled(true);
            }
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
    }
});
你插进去怎么样

if (menu.itemList.getSelectedValue() == null) 
    return; 
以前

String index = menu.itemList.getSelectedValue().toString();

如果未选择任何内容,则您不想执行任何操作。

看起来getSelectedValue()返回null。您没有显示第63行中的内容。“在de.tdm.app.Mainframe$1.valueChanged(Mainframe.java:63)”这就是你的代码爆炸的地方。@塔里克:它在第一个代码块的第4行-看看那里的注释。@Y256:你是对的,看起来在某些情况下,这恰好返回null。如果我能找到原因,我会调查的。我猜setSelectedValue(…)的工作方式与预期的不一样……如果(menu.itemList.getSelectedValue()==null)返回,那么插入
if(menu.itemList.getSelectedValue()==null)如何
before
String index=menu.itemList.getSelectedValue().toString()
String index = menu.itemList.getSelectedValue().toString();