首页 > 解决方案 > label.setText 上的 java.lang.reflect.InvocationTargetException

问题描述

我确定我离答案很近,但我再也看不到我的错误了。

我正在尝试为 listView 加载自定义 listCell。

问题是,当我使用

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();
                }

“...”/target/classes/articleCell.fxml:8 上的 LoadException,我认为它是由缺少的 fx:controller 产生的...但是...这是没有意义的...对吗?

标签: javajavafxfxml

解决方案


推荐阅读