NullPointerException 尽管 fx:id 和 fx:controller 是正确的

NullPointerException despite the fx:id and fx:controller are correct

我正在为我的局域网应用创建 table 日志。当我尝试 运行 这个应用程序时,它抛出 NullPointerException,但是 fx:id 和 fx:controller 是正确的。我正在使用 JavaFX 12。

我尝试用 Intellij 重构 fx:id 和 fx:controller,Intellij 在 logPane.fxml 和 LogPaneController.class 中也正确地更改了这一点,但它仍然无法正常工作。如果我注释掉异常所在的行,则所有应用程序都可以正常工作。我用谷歌搜索短语 "FXML component returns NUllPointerException",但结果始终是 "your fx:id or fx:controller name is not the same in controller"。

logPane.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane HBox.hgrow="ALWAYS" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="com.lanssmaker.controller.LogPaneController">
    <children>
        <TableView fx:id="logTable" prefHeight="574.0" prefWidth="382.0" AnchorPane.bottomAnchor="0.0"
                   AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <columns>
                <TableColumn maxWidth="140.0" minWidth="70" prefWidth="70.0" text="Time"/>
                <TableColumn text="Content"/>
                <TableColumn maxWidth="140.0" minWidth="70" prefWidth="70.0" text="Category"/>
            </columns>
            <columnResizePolicy>
                <TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
            </columnResizePolicy>
        </TableView>
    </children>
</AnchorPane>

LogPaneController.class

public class LogPaneController {

    @FXML
    private TableView<Log> logTable; //it's null

    public TableView<Log> getLogTableView() {
        return logTable;
    }


    public void initialize() {
        //example ussage calling NullPointerException
        logTable.isHover();
    }
}

异常日志

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:567)
 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:567)
 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(LauncherImpl.java:195)
 at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: javafx.fxml.LoadException: 
/D:/Programming/Java/javastart/ssmaker/target/classes/fxml/buttonsPane.fxml
/D:/Programming/Java/javastart/ssmaker/target/classes/fxml/mainPane.fxml:17

 at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)
 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
 at javafx.fxml/javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1154)
 at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
 at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
 at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
 at ssmaker/com.lanssmaker.main.Main.start(Main.java:16)
 at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1(LauncherImpl.java:846)
 at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:455)
 at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:428)
 at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
 at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater(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(WinApplication.java:174)
 ... 1 more
Caused by: 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:567)
 at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
 at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.base/java.lang.reflect.Method.invoke(Method.java:567)
 at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
 at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
 at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2591)
 ... 22 more
Caused by: java.lang.NullPointerException
 at ssmaker/com.lanssmaker.controller.LogPaneController.initialize(LogPaneController.java:25)
 ... 33 more

package com.lanssmaker.main;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;



public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Pane mainPane = FXMLLoader.load(getClass().getResource("/fxml/mainPane.fxml"));
        Scene scene = new Scene(mainPane);
        stage.setScene(scene);
        stage.setTitle("LAN ScreenShots Maker");
        stage.show();
    }
}

mainPane.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity"
            minWidth="-Infinity" prefHeight="624.0" prefWidth="813.0" xmlns="http://javafx.com/javafx/11.0.1"
            xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.lanssmaker.controller.MainController">
    <children>
        <HBox layoutX="158.0" layoutY="143.0" prefHeight="624.0" prefWidth="813.0" AnchorPane.bottomAnchor="25.0"
              AnchorPane.leftAnchor="25.0" AnchorPane.rightAnchor="25.0" AnchorPane.topAnchor="25.0">
            <children>
                <AnchorPane HBox.hgrow="NEVER">
                    <children>
                        <VBox prefHeight="574.0" prefWidth="389.0" AnchorPane.bottomAnchor="0.0"
                              AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="0.0">
                            <children>
                                <fx:include source="connectionPane.fxml"/>
                                <fx:include source="buttonsPane.fxml"/>
                            </children>
                        </VBox>
                    </children>
                </AnchorPane>
                <fx:include source="logPane.fxml"/>
            </children>
        </HBox>
    </children>
</AnchorPane>

这里是Project structure

您有 getter class 用于私有 logTable,因此您可以获得一个值,但我没有看到值初始化或 setter class(这将初始化值)。

仔细查看堆栈跟踪:

...
Caused by: javafx.fxml.LoadException: 
/D:/Programming/Java/javastart/ssmaker/target/classes/fxml/buttonsPane.fxml
/D:/Programming/Java/javastart/ssmaker/target/classes/fxml/mainPane.fxml:17
...
Caused by: java.lang.NullPointerException
    at ssmaker/com.lanssmaker.controller.LogPaneController.initialize(LogPaneController.java:25)

这意味着当您加载 buttonsPane.fxmlFXMLLoader 处理

时会发生异常
<fx:include source="buttonsPane.fxml"/>

元素。

此外,它告诉我您不仅将 LogPaneController 用于 logPane.fxml,而且还用于 buttonsPane.fxml。每次加载 fxml 时都会创建单独的控制器实例;使用 <fx:include> 会创建一个单独的 FXMLLoader 实例来加载包含的 fxml。 logTable 字段仅为其中之一注入;对于另一个控制器实例,它仍然是 null。 (这甚至忽略了加载 buttonsPane.fxml 发生在加载 logPane.fxml 之前的事实,所以即使在加载 buttonsPane.fxml 完成时使用了相同的控制器实例, TableView 也没有基于 logPane.fxml.)

创建

buttonsPane.fxmllogPane.fxml 使用单独的控制器 classes。您不会通过简单地使用相同的控制器 class 来获得两个 fxml "to communicate"。我会建议一个替代方案,但你在那里做的事情没有任何意义(除非你试图挑起 NPE):isHover 只是重新调整 hover [=44= 的值] 对于尚未成为 Scene.

一部分的节点,它始终是 false