添加到列表时的 JavaFX ListView 空指针

JavaFX ListView Null Pointer when Adding to List

编辑:静电已移除。仍然出现相同的错误

我已经在场景生成器中创建了一个带有列表视图的 GUI,现在我正在尝试用一些字符串填充列表视图。

我目前正在使用 2 类。这里有 2 个简化版本的脚本,以便于阅读 主控制器:

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


public class MainController extends Application
{
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("IntelligentSystems.fxml"));
        primaryStage.setTitle("Intelligent Systems Agent Program");
        primaryStage.setScene(new Scene (root));
        primaryStage.show();

        GUIController GUIList = new GUIController();
        GUIList.DoList.add("agentCtrl");
        GUIList.DoList.add("DeliveryAgent");
        GUIList.PopulateAgentList();
    }

    public static void main (String[] args)
    {
        launch(args);
    }
}

和第二个Class,GUIController:

import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;

public class GUIController {
    //List View variables.
    @FXML
    public ListView AgentsList;

    @FXML
    public ObservableList<String> DoList= FXCollections.observableArrayList();

    @FXML
    public void PopulateAgentList()
    {
        AgentsList.setItems(DoList);
    }
}

目前,我在 fxml 文件中将 Listview 的 fxId 设置为 "AgentsList"。

<ListView fx:id="AgentsList" layoutX="15.0" layoutY="39.0" prefHeight="200.0" prefWidth="86.0" />

每当我 运行 IntelliJ 中的程序时,它总是 returns java.lang.NullPointerException 围绕 "AgentsList.setItems(_doList);" 行。

我完全不知道为什么它不能添加到列表中。由于我主要使用 C#,所以我没有使用 Java 并没有帮助。

编辑:异常堆栈跟踪:

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:830)
Caused by: java.lang.NullPointerException
    at GUIController.PopulateAgentList(GUIController.java:26)
    at MainController.start(MainController.java:57)
    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:391)
    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
Exception running application MainController

Process finished with exit code 1

您的 @FXML 注释字段是静态的,JavaFX 不支持它,因为它被设计为 create/load 同一 fxml file/controller class 的多个实例。如果你不了解'static'具体是什么意思,可以参考What is the difference between a static method and a non-static method?.

至于如何获取正确的控制器实例,需要加载的fxml文件稍有不同。

FXMLLoader loader = new FXMLLoader(getClass().getResource("IntelligentSystems.fxml"));
Parent root = loader.load(); // must be called before getting the controller!
GUIController controller = loader.getController();

现在您可以使用 controller 变量以非静态方式访问字段。