当ListSelectionListener正在运行时,JList中的空指针异常

时间:2012-02-08 22:12:24

标签: java swing nullpointerexception jlist

我写了一些在JLabel上设置Image的代码。 Image的路径是从JList上所选项的getSelectedValue()方法获得的。

当用户点击搜索按钮时,通过在JComboBox中选择三个项目中的任何选项(即typeChooserBox)后在JTextField(即searchTextField)中键入要搜索的项目来填充Jlist。然后我的程序从文件“Records.txt”中读取已保存的记录并分成令牌以比较正确的令牌(基于typeChooserBox的索引)和用户输入值(在searchTextField中)。然后它填充JList。

下面是填充JList的搜索Button的事件处理程序。

    public class searchButtonListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
       model.clear();//This empties the JList creating nullpoint Ex
        int index=typeChooserBox.getSelectedIndex();
        String toCompare=searchTextField.getText();

        try {
                        File file = new File("Records.txt");
        BufferedReader reader = new BufferedReader(new FileReader(file));

            String line=null;
                while((line=reader.readLine())!=null){
                    String[] tokens = line.split("/");


                    if( index==0){
                        if(tokens[0].equals(toCompare))
                            model.addElement(tokens[2]);}

                    if(index==1){
                        if(tokens[1].equalsIgnoreCase(toCompare))
                            model.addElement(tokens[2]);}
                    if(index==2){
                        if(tokens[3].contains(toCompare))
                            model.addElement(tokens[2]);}
                                    }
        }
        catch(FileNotFoundException e2){
    JOptionPane.showMessageDialog(null,"File not found", "Error",JOptionPane.ERROR_MESSAGE);
    }
        catch(IOException ex){
            JOptionPane.showMessageDialog(null,"File not found", "Error",JOptionPane.ERROR_MESSAGE);
        }

}

}

当我搜索记录时。对于第一次 - 我的JList被填充了搜索结果。然后,当我从JList中选择一个项目时,它可以工作(在JLabel上设置正确的图像),直到我执行新的搜索。只要我点击搜索按钮(第二次)。我的程序抛出nullPointExecption并停止工作。对不起,我没有加入SSCCE。但如果这些小信息不充分,请告诉我。 :)

我能解决我的问题...... 代码model.clear()在单击搜索按钮时清除列表中的所有项目。由于这个原因,列表中将没有选定的项目导致空指针异常。但是我该如何解决这个问题。我可以清除所有字段,但在为JList调用getSelctedValue()时避免使用null值。

我尝试过这样做,但它仍然没有用。

    public class searchListListener implements ListSelectionListener {
        String s;
        String imagePath;


        public void valueChanged(ListSelectionEvent evt){ try{

         imagePath= (String) searchResult.getSelectedValue();

        ImageIcon image = new ImageIcon(imagePath);
        imageLabel.setIcon(image);
        searchResult.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);




    }
        catch(NullPointerException ne){
             JOptionPane.showMessageDialog(null, "NullPointerException");   
             model.addElement(s);
             searchResult.setSelectedValue(s, true);

            }
        finally{
            s=imagePath;
        }
}
    }




public class typeChooserBoxListener implements ItemListener{
    public void itemStateChanged(ItemEvent ev){

    }
}

我是这个java世界的新手,很抱歉,如果我无法帮助你帮助我。:(

1 个答案:

答案 0 :(得分:1)

如果没有您的异常的堆栈跟踪或者searchResult.getSelectedValue()中发生的情况,很难说清楚。

也许试试这个:

public void valueChanged(ListSelectionEvent evt){
    if( evt.getValueIsAdjusting() ) return;

    // your code here

}