[英]Getting Null Pointer exception and Can't understand why
在主ListView
上下滚动时,我的程序似乎运行良好。 当我打开TitledPane
并使用鼠标滚轮快速向下或向上滚动时,就会出现问题。 我还注意到,如果我在打开TitledPane
后使用鼠标拖动滚动条,一切正常。 我想我很擅长发现和修复我的NullPointer
错误,但这让我感到困惑。 我如何查明错误的原因以及如何修复它。 如果我能理解发生了什么,我可能会弄清楚第二部分。
主要的
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class NPEDiggingSO extends Application
{
private static class OuterListCell extends ListCell<MainListViewCellData>
{
private final ListView<Note> cellListView;
public OuterListCell()
{
setPrefHeight(300);
setPrefWidth(300);
cellListView = new ListView<>();
cellListView.setCellFactory(v -> new NoteCell());
}
@Override
protected void updateItem(MainListViewCellData item, boolean empty)
{
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
setGraphic(null);
}
else {
cellListView.getItems().setAll(item.getNotes());
setGraphic(cellListView);
}
}
}
private Parent createContent()
{
DataModel model = new DataModel();
ListView<MainListViewCellData> outer = new ListView<>(model.getMainListViewData());
outer.setCellFactory(c -> new OuterListCell());
BorderPane content = new BorderPane(outer);
return content;
}
@Override
public void start(Stage stage) throws Exception
{
stage.setScene(new Scene(createContent(), 700, 500));
//stage.setTitle(FXUtils.version());
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
数据模型
import java.util.ArrayList;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
/**
*
* @author blj0011
*/
public class DataModel
{
public DataModel()
{
}
public ObservableList<MainListViewCellData> getMainListViewData()
{
ObservableList<MainListViewCellData> observableList = FXCollections.observableArrayList();
for (int i = 0; i < 250; i++) {
MainListViewCellData mainListViewCellData = new MainListViewCellData();
List<Note> notes = new ArrayList();
notes.add(new Note("note title " + i, "note text " + 1));
mainListViewCellData.setNotes(notes);
observableList.add(mainListViewCellData);
}
return observableList;
}
}
MainListViewCellData
import java.util.List;
import javafx.collections.FXCollections;
/**
*
* @author blj0011
*/
public class MainListViewCellData
{
private List<Note> notes;
public MainListViewCellData(List<Note> notes)
{
this.notes = notes;
}
public MainListViewCellData()
{
this.notes = FXCollections.observableArrayList();
}
public List<Note> getNotes()
{
return notes;
}
public void setNotes(List<Note> notes)
{
this.notes = notes;
}
@Override
public String toString()
{
return '{' + "notes=" + notes + '}';
}
}
笔记
public class Note
{
private String title;
private String text;
public Note(String title, String text)
{
this.title = title;
this.text = text;
}
public String getText()
{
return text;
}
public void setText(String text)
{
this.text = text;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(", title=").append(title);
sb.append(", text=").append(text);
sb.append('}');
return sb.toString();
}
}
NoteCell
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.ListCell;
import javafx.scene.control.TextArea;
import javafx.scene.control.TitledPane;
public class NoteCell extends ListCell<Note>
{
TextArea textArea = new TextArea();
TitledPane titledPane = new TitledPane("", textArea);
ObservableList<Note> observableList = FXCollections.observableArrayList();
@Override
public void updateItem(Note item, boolean empty)
{
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
setGraphic(null);
}
else {
titledPane.setExpanded(false);
titledPane.setText(item.getTitle());
titledPane.setAnimated(false);
textArea.setText(item.getText());
setGraphic(titledPane);
}
}
}
错误信息
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.scene.control.skin.VirtualFlow$4.findOwnerCell(VirtualFlow.java:848)
at com.sun.javafx.scene.control.skin.VirtualFlow$4.select(VirtualFlow.java:822)
at com.sun.javafx.scene.traversal.TraversalEngine.select(TraversalEngine.java:103)
at com.sun.javafx.scene.traversal.TopMostTraversalEngine.trav(TopMostTraversalEngine.java:77)
at javafx.scene.Scene.traverse(Scene.java:2005)
at javafx.scene.Scene.focusIneligible(Scene.java:2024)
at javafx.scene.Scene.access$3400(Scene.java:159)
at javafx.scene.Scene$ScenePulseListener.focusCleanup(Scene.java:2370)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2385)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Toolkit.java:398)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:561)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:541)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:534)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:340)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
聚苯乙烯
我想为我的懒惰道歉,并感谢@kleopatra 在我有机会之前创建了 MCVE。
没有解决方案,只是一个 MCVE 和一些观察(我的上下文是 Win10,fx11)。 要查看错误,请单击最顶部单元格中的按钮,然后通过鼠标滚轮向下滚动。 错误
所以,是的,我会说这是关于 focusTraversal 的错误 - 但无法解决它。
这个例子:
public class NPEDiggingSO extends Application {
private static class InnerListCell extends ListCell<String> {
private Button button;
public InnerListCell() {
button = new Button();
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
setGraphic(null);
} else {
button.setText(item);
setGraphic(button);
}
}
}
private static class OuterListCell extends ListCell<String> {
private ListView<String> cellListView;
public OuterListCell() {
setPrefHeight(300);
setPrefWidth(300);
cellListView = new ListView<>();
cellListView.setCellFactory(c -> new InnerListCell());
}
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
setGraphic(null);
} else {
cellListView.getItems().setAll(item);
setGraphic(cellListView);
}
}
}
private Parent createContent() {
ObservableList<String> model = FXCollections.observableArrayList(
"item1", "item2", "item3", "item4", "item5");
ListView<String> outer = new ListView<>(model);
outer.setCellFactory(c -> new OuterListCell());
BorderPane content = new BorderPane(outer);
return content;
}
@Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(createContent()));
stage.setTitle(FXUtils.version());
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.