简体   繁体   English

从JList中删除项目会引发NullPointerException

[英]Remove Item from JList throws NullPointerException

NetBeans. NetBeans。 UI created using IDE. 使用IDE创建的UI。 My implementation In order of appearance: 我的实现按出现顺序:

1 global form variable: 1个全局形式变量:

private DefaultListModel model;

2 constructor: 2个构造函数:

ArrayList<String> cameras = repository.getCameraNames();

model= new DefaultListModel();
for (int i = 0; i < cameras.size(); i++) {
    model.addElement(cameras.get(i));
}
thelist.setModel(model);

3 remove button: 3移除按钮:

private void btnRemoverActionPerformed(java.awt.event.ActionEvent evt) {                                           
    int index = thelist.getSelectedIndex();

    model.removeElementAt(index);
}

On the removeElementAt line, I get NullPointerException. 在removeElementAt行上,我得到NullPointerException。 If i change the index for a explicit number, it works but doesn't with the index variable. 如果我将索引更改为一个明确的数字,它可以工作,但不能使用index变量。 But the selected index, doesn't work! 但是选定的索引不起作用!

Anyone can help? 有人可以帮忙吗?

Oh, and here is some stackttrace: 哦,这是一些stackttrace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at FrmPrincipal.thelistValueChanged(FrmPrincipal.java:217)
at FrmPrincipal.access$000(FrmPrincipal.java:22)
at FrmPrincipal$1.valueChanged(FrmPrincipal.java:77)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1798)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1812)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
at javax.swing.DefaultListSelectionModel.removeIndexInterval(DefaultListSelectionModel.java:677)
at javax.swing.plaf.basic.BasicListUI$Handler.intervalRemoved(BasicListUI.java:2601)
at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:179)
at javax.swing.DefaultListModel.removeElementAt(DefaultListModel.java:332)

UPDATE 更新

I fixed by changing the following method: 我通过更改以下方法进行修复:

private void thelistValueChanged(javax.swing.event.ListSelectionEvent evt) {
    txtCameraName.setText(thelist.getSelectedValue().toString());
}

To

private void thelistValueChanged(javax.swing.event.ListSelectionEvent evt) {
    txtCameraName.setText((String)thelist.getSelectedValue());
}

But I don't know why! 但是我不知道为什么! Can someone explain why it didn't accept the toString() and accepted the casting? 有人可以解释为什么它不接受toString()并接受强制转换吗?

Your change "fixed" your problem because getSelectedValue().toString() will throw a NPE if there is no selection, whereas (String)getSelectedValue() will evaluate to null if there is no selection. 您的更改“解决”了您的问题,因为如果没有选择,则getSelectedValue().toString()将抛出NPE,而如果没有选择,则(String)getSelectedValue()将评估为null But, if you ever put things in your list that aren't String s, then you'll get ClassCastException s when you try to cast the selected value to a String . 但是,如果您曾经将不是String的内容放到列表中,那么当您尝试将选定的值转换为String时,将得到ClassCastException So, this may have solved your current problem, but it's not a solution really. 因此,这可能已经解决了您当前的问题,但这并不是真正的解决方案。

I would just do this: 我会这样做:

private void theListValueChanged(ListSelectionEvent e) {
    final Object selectedValue = theList.getSelectedValue();
    if ( selectedValue != null ) {
        txtCameraName.setText( selectedValue.toString() );
    } else {
        // Clear the text since there's no selection
        txtCameraName.setText( null );
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM