为什么对 JList.clearSelection() 的调用会给我一个空指针异常?

发布于 2024-10-17 23:47:23 字数 953 浏览 3 评论 0原文

这是我第一次发帖,但我尝试遵循指南并检查之前的问题等,因此我们将不胜感激。

我有一个名为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我喜欢麦丽素 2024-10-24 23:47:24
if (select.getValueIsAdjusting()) {

作为猜测,我认为上面的代码行应该是如果值没有调整

或者如果不起作用,则尝试将clearSelection()代码包装在SwingUtilities.invokeLater()中。

实际上问题可能是当您清除选择时会生成另一个 valueChanged 事件。这次 getSelectedValue 将返回空值,因为没有选择任何内容。因此,您可能需要在某处添加 if 条件来处理此问题。

否则需要 SSCCE。

if (select.getValueIsAdjusting()) {

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文