【问题标题】:JavaFX ListView Null Pointer when Adding to List [duplicate]添加到列表时的JavaFX ListView空指针[重复]
【发布时间】:2020-01-30 07:48:04
【问题描述】:

编辑:静态已被移除。还是出现同样的错误

我在 Scene builder 中创建了一个带有 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行。

我完全不知道为什么它不能添加到列表中。由于我大部分时间都在使用 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 会注入静态。 (而且您到处都在遵循 C# 而不是 Java 命名约定。)
  • 遵循 IntellIJ 的修复建议。试图让它从另一个类引用列表视图,它说不能从静态上下文引用非静态字段。老实说,想避免使用这么多的静态变量,但 IntelliJ 在添加静态变量时删除了错误消息。

标签: java listview javafx nullpointerexception observablelist


【解决方案1】:

您的 @FXML 注释字段是静态的,JavaFX 不支持,因为它旨在创建/加载同一 fxml 文件/控制器类的多个实例。如果您不熟悉“静态”的实际含义,可以参考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 变量以非静态方式访问字段。

【讨论】:

  • 那行得通。太感谢了。也会查看该链接以熟悉差异。
猜你喜欢
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多