JavaFX 列表视图 添加到列表时的空指针



编辑:静态已被删除。仍然收到相同的错误

我已经在场景构建器中创建了一个带有列表视图的 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);
}
}

第二类,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 文件中将列表视图的 fxId 设置为"代理列表"。

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

每当我在IntelliJ中运行该程序时,它总是在"AgentsList.setItems(_doList(;"行周围返回java.lang.NullPointerException。

我完全不知道为什么它不能添加到列表中。这无济于事,因为我大部分时间都在使用 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$2(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$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 MainController
Process finished with exit code 1

您的@FXML注释字段是静态的,JavaFX 不支持该字段,因为它旨在创建/加载同一 fxml 文件/控制器类的多个实例。如果你不熟悉"静态"的实际含义,你可以参考静态方法和非静态方法有什么区别?。

至于如何获得正确的控制器实例,您需要加载稍微不同的 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变量以非静态方式访问字段。

最新更新