java - 加载 fxml 文档后无法执行控制器方法
问题描述
我正在尝试将 AnchorPane 添加到我的 fxml 文件中,在通过我在控制器中定义的方法将它加载到 main 之后,但我收到了这个错误
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at sample.Controller.addAnchor(Controller.java:20)
at sample.Main.start(Main.java:16)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Exception running application sample.Main
控制器.java
public class Controller {
VBox vBox;
public void addAnchor(){
AnchorPane user = new AnchorPane();
user.setPrefHeight(50);
user.setPrefWidth(200);
Label test = new Label("test");
test.setLayoutX(113);
test.setLayoutY(15);
test.setCursor(Cursor.HAND);
user.setCursor(Cursor.HAND);
user.getChildren().add(test);
vBox.getChildren().add(user);
}
}
主.java
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
loader.setController(new Controller());
Controller controller = loader.getController();
controller.addAnchor();
Parent root = loader.load();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}
网格窗格
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="572.0" prefWidth="755.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
盒子
<VBox id="vBox" maxHeight="1.7976931348623157E308" prefHeight="444.0" prefWidth="252.0" style="-fx-background-color: #C3B091;">
我在这里被困了一天,任何帮助将不胜感激。
解决方案
推荐阅读
- javascript - 如何将 InstancedBufferGeometry 与 threejs 一起使用
- tikz - 在 Tikz 中设置默认绘图
- java - 如何解决 Android Studio 中的私有布尔错误
- html - 如何在屏幕右边缘显示下拉菜单或任何 div
- android - 在 Flutter 中使用多个配置文件
- python - 信息 Schema.columns 返回不同的字符
- postgresql - ConnectionAcquireTimeoutError [SequelizeConnectionAcquireTimeoutError]:操作超时
- angularjs - Angular 前端和 Laravel api 的 Nginx 设置
- vscode-extensions - 使用 TreeDataProvider 中的“vscode.open”命令
- javascript - Discord.js 权限