为什么对 JList.clearSelection() 的调用会给我一个空指针异常?
这是我第一次发帖,但我尝试遵循指南并检查之前的问题等,因此我们将不胜感激。
我有一个名为history 的JList,它包含一个表示简单数学方程(即2+2)的字符串列表。我想做到这一点,以便当您单击列表中的某个项目时,它会与输入字段中的另一个字符串连接起来,当我不尝试清除选择时,我的所有方法都可以工作(以便相同的选择可以多次)但是当我尝试使用清除选择方法时,它会抛出一个空指针。这是我调用它的代码,
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class HistoryListener implements ListSelectionListener {
JList history;
ListSelectionModel historySelection;
public HistoryListener(JList history) {
this.history = history;
historySelection = history.getSelectionModel();
}
@Override
public void valueChanged(ListSelectionEvent select) {
if (select.getValueIsAdjusting()) {
NumListener.addHistory(history.getSelectedValue().toString());
}
if (history.isSelectionEmpty() == false) {
history.clearSelection();
}
}
}
如果您需要更多信息,请告诉我
干杯
this is the first time that I've posted but I tried to follow the guidelines and checked previous questions etc so any help on this would be much appreciated.
I have a JList called history which has a holds a list of strings representing simple mathematical equations (ie 2+2). I want to make it so that when you click on an item in the list it is then concatenated with another string in an input field, all of my methods work when I don't try to clear the selection (so that the same selection can be made multiple times) but when I try to use the clear selection method it throws a null pointer. here's the code where I'm calling it
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class HistoryListener implements ListSelectionListener {
JList history;
ListSelectionModel historySelection;
public HistoryListener(JList history) {
this.history = history;
historySelection = history.getSelectionModel();
}
@Override
public void valueChanged(ListSelectionEvent select) {
if (select.getValueIsAdjusting()) {
NumListener.addHistory(history.getSelectedValue().toString());
}
if (history.isSelectionEmpty() == false) {
history.clearSelection();
}
}
}
if you need anymore info please let me know
cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为猜测,我认为上面的代码行应该是如果值没有调整
或者如果不起作用,则尝试将clearSelection()代码包装在SwingUtilities.invokeLater()中。
实际上问题可能是当您清除选择时会生成另一个 valueChanged 事件。这次 getSelectedValue 将返回空值,因为没有选择任何内容。因此,您可能需要在某处添加 if 条件来处理此问题。
否则需要 SSCCE。
As a guess, I think the above line of code should be if the value is NOT adjusting
Or if that doesn't work, then try wrapping the clearSelection() code in a SwingUtilities.invokeLater().
Actually the problem may be that when you clear the selection another valueChanged event is generated. This time the getSelectedValue will return a null value since nothing is selected. So you will probably need to add an if condition somewhere to handle this.
Otherwise a SSCCE is needed.