Я уверен, что близок к ответам, но больше не вижу своих ошибок.
Я пытаюсь загрузить пользовательский listCell для listView.
Проблема в том, что когда я использую
labelArticle.setText(article.getNewsSite());
Это просто исключение, указанное ниже. Значит, я получаю его для каждого нового listCell.
Контроллер:
public class ArticleCellController extends ListCell<Article> {
@FXML
private ImageView icon;
@FXML
private Label labelArticle;
@FXML
private Label titleArticle;
@FXML
private Label resumeArticle;
@FXML
private GridPane gridPane;
private FXMLLoader mLLoader;
@Override
protected void updateItem(Article article, boolean empty) {
super.updateItem(article, empty);
if (empty || article == null) {
setText(null);
setGraphic(null);
} else {
if (mLLoader == null) {
mLLoader = new FXMLLoader(getClass().getResource("/articleCell.fxml"));
mLLoader.setController(this);
try {
mLLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
}
labelArticle.setText(article.getNewsSite());
titleArticle.setText(article.getTitle());
resumeArticle.setText(article.getSummary());
// setText(null);
setGraphic(gridPane);
}
}
}
Файл fxml:
<?xml version = "1.0" encoding = "UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane fx:id = "gridPane" maxHeight = "-Infinity" maxWidth = "-Infinity" minHeight = "-Infinity" minWidth = "-Infinity" xmlns = "http://javafx.com/javafx/8.0.65" xmlns:fx = "http://javafx.com/fxml/1">
<columnConstraints>
<ColumnConstraints halignment = "CENTER" hgrow = "SOMETIMES" maxWidth = "30.0" minWidth = "10.0" prefWidth = "30.0" />
<ColumnConstraints hgrow = "SOMETIMES" maxWidth = "120.0" minWidth = "10.0" prefWidth = "30.0" />
<ColumnConstraints halignment = "CENTER" hgrow = "SOMETIMES" maxWidth = "31.0" minWidth = "10.0" prefWidth = "30.0" />
<ColumnConstraints hgrow = "SOMETIMES" minWidth = "10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight = "10.0" prefHeight = "30.0" vgrow = "SOMETIMES" />
</rowConstraints>
<children>
<Label fx:id = "labelArticle" text = "Label" GridPane.columnIndex = "1" />
<Label fx:id = "titleArticle" text = "Label" GridPane.columnIndex = "1" />
<Label fx:id = "resumeArticle" text = "Label" GridPane.columnIndex = "3" />
</children>
</GridPane>
Ошибка:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.NullPointerException
at ch.bfh.spacenews/ch.bfh.spacenews.ArticleCellController.updateItem(ArticleCellController.java:46)
at ch.bfh.spacenews/ch.bfh.spacenews.ArticleCellController.updateItem(ArticleCellController.java:14)
at javafx.controls/javafx.scene.control.ListCell.updateItem(ListCell.java:478)
at javafx.controls/javafx.scene.control.ListCell.indexChanged(ListCell.java:337)
at javafx.controls/javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:120)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1742)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1726)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1852)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2755)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1245)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1207)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1214)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1214)
at javafx.graphics/javafx.scene.Parent.layout(Parent.java:1214)
at javafx.graphics/javafx.scene.Scene.doLayoutPass(Scene.java:576)
at javafx.graphics/javafx.scene.Scene.preferredSize(Scene.java:1784)
at javafx.graphics/javafx.scene.Scene$2.preferredSize(Scene.java:393)
at javafx.graphics/com.sun.javafx.scene.SceneHelper.preferredSize(SceneHelper.java:66)
at javafx.graphics/javafx.stage.Window$12.invalidated(Window.java:1086)
at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:110)
at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
at javafx.graphics/javafx.stage.Window.setShowing(Window.java:1174)
at javafx.graphics/javafx.stage.Window.show(Window.java:1189)
at javafx.graphics/javafx.stage.Stage.show(Stage.java:273)
at ch.bfh.spacenews/ch.bfh.spacenews.MainFX.start(MainFX.java:38)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Exception running application ch.bfh.spacenews.MainFX
Проверил этот пост java.lang.reflect.InvocationTargetException, когда я пытаюсь изменить текстовое значение метки
Но не забыл раскомментировать mLLoader.load();
Так что же могло пойти не так?
Обновлять : С отладчиком у меня первая ошибка в try-catch:
try {
mLLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
LoadException на "..." / target / classes / articleCell.fxml: 8, которое, как я предполагаю, вызвано отсутствующим контроллером fx: ... но ... это бессмысленно ... верно?
Наиболее важным исключением обычно является исключение сокровенный; здесь это NullPointerException в строке 46 вашего класса ArticleCellController. Вот в чем проблема: ваш `labelArticle` никогда не устанавливается.
@rzwitserloot Но я так не думаю, потому что он должен быть установлен на шаблон fxml с fx: id




Я бы не рекомендовал
extends ListCell<Article>. РасширениеControllerнаListCellкажется мне плохой практикой.