添加到列表时 JavaFX ListView 空指针 [duplicate]

JavaFX ListView Null Pointer when Adding to List [duplicate]

提问人:Cfusion 提问时间:10/1/2019 最后编辑:Cfusion 更新时间:10/1/2019 访问量:1602

问:

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

我已经在场景构建器中创建了一个带有 Listview 的 GUI,现在我正在尝试用一些字符串填充 Listview。

我目前使用 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 文件中将 Listview 的 fxId 设置为“AgentsList”。

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

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

我完全不明白为什么它不能添加到列表中。我没有长时间使用 Java,因为我主要使用 C#,这无济于事。

编辑:异常堆栈跟踪:

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
java listview javafx nullpointerexception observablelist

评论

0赞 Kaan 10/1/2019
如果您发布整个异常堆栈跟踪,这将很有帮助。
0赞 Cfusion 10/1/2019
等一下,我现在补充一下
1赞 chrylis -cautiouslyoptimistic- 10/1/2019
你为什么要让一切都静态?我不认为会注入静电。(而且你到处都遵循 C# 而不是 Java 命名约定。@FXML
0赞 Cfusion 10/1/2019
遵循 IntellIJ 的修复建议。试图让它从另一个类引用列表视图,它说不能从静态上下文引用非静态字段。老实说,想要避免使用这么多静态,但 IntelliJ 在添加静态时删除了错误消息。

答:

3赞 n247s 10/1/2019 #1

带注释的字段是静态的,JavaFX 不支持这一点,因为它旨在创建/加载同一 fxml 文件/控制器类的多个实例。如果你不熟悉“静态”到底是什么意思,可以参考静态方法和非静态方法有什么区别?@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

评论

0赞 Cfusion 10/1/2019
这奏效了。非常感谢。还将查看该链接以熟悉其中的差异。