gpt4 book ai didi

java - 为什么listView.getSelectionModel().getSelectedIndex()返回-1?

转载 作者:行者123 更新时间:2023-12-01 21:26:07 25 4
gpt4 key购买 nike

在过去的几天里,这导致我在运行时出现大量错误- 并发修改异常和空指针异常

我不记得我添加了什么,但在这个段中添加了一段代码,它抛出了一个数组越界异常,导致我发现它在抛出的空指针异常之上抛出了一个负索引viewList 中没有任何内容。

为什么要这样做?这周我刚刚开始摆弄 javafx,这让我相信我一定在某个地方搞砸了?

ObservableList<BusinessCard> observableList = FXCollections.observableArrayList(); //initialized above

// ListView Listener, changes text fields for the selected B.C in ViewList
try {
listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<BusinessCard>() {
@Override
public void changed(ObservableValue<? extends BusinessCard> arg0, BusinessCard oldval,BusinessCard newVal) {
int index = listView.getSelectionModel().getSelectedIndex();

if(index == -1) return; //Fix

BusinessCard card = observableList.get(index);
setDataFields(card);
}
});
} catch (NullPointerException e1) {
// Do nothing, NullPointer thrown when list is empty
}

最佳答案

我认为documentation for the selectedIndex property这里说得很清楚了。

Refers to the selected index property, which is used to indicate the currently selected index value in the selection model. The selected index is either -1, to represent that there is no selection, or an integer value that is within the range of the underlying data model size.

关于java - 为什么listView.getSelectionModel().getSelectedIndex()返回-1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38125241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com