提问人:Cfusion 提问时间:10/1/2019 最后编辑:Cfusion 更新时间:10/1/2019 访问量:1602
添加到列表时 JavaFX ListView 空指针 [duplicate]
JavaFX ListView Null Pointer when Adding to List [duplicate]
问:
编辑:删除了静态。仍然收到相同的错误
我已经在场景构建器中创建了一个带有 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
答:
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
这奏效了。非常感谢。还将查看该链接以熟悉其中的差异。
评论
@FXML