加载fxml文档后无法执行控制器方法

在通过控制器中定义的方法将其加载到main中之后,我试图将AnchorPane添加到我的fxml文件中,但是出现此错误

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

Controller.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);
    }
}

Main.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

<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

<VBox id="vBox" maxHeight="1.7976931348623157E308" prefHeight="444.0" prefWidth="252.0" style="-fx-background-color: #C3B091;">

Project structure

我在这里呆了一天,任何帮助将不胜感激。

iCMS 回答:加载fxml文档后无法执行控制器方法

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2164036.html

大家都在问