【问题标题】:JavaFX - Error while trying to add function to start functionJavaFX - 尝试添加功能以启动功能时出错
【发布时间】:2016-06-25 00:00:22
【问题描述】:

我有一个我可能看不到的问题。

主题:我有我的类Main,其中包含窗口,我有一个类MainController,我将我的所有函数放在类Main

Main:

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {

        Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        // primaryStage.setFullScreen(true);
        primaryStage.show();
        MainController m = new MainController();
        m.testFunction();
    }
}

班级MainController:

public class MainController {
    @FXML private Label answer1;
    @FXML private Label answer2;

    public void testFunction()
    {
        answer1.setText("FIRST");
        answer2.setText("SECOND");
    }
}

Main.start 中调用testFunction 时会出现此错误:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in     Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at application.MainController.testFunction(MainController.java:43)
at application.Main.start(Main.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
... 1 more

我尝试调试两天没有成功。

感谢你们的帮助!

【问题讨论】:

  • MainController 类的第 43 行是哪一行?
  • 可能控制器的元素还没有初始化,你得到 NullPointer Exception 。检查 Merve Sahin 的答案。

标签: java javafx nullpointerexception runtimeexception


【解决方案1】:

你的 MainController 应该实现 Initializable 并且你应该在你的 fxml 中设置控制器: 例如

<AnchorPane fx:controller="yourpackagename.MainController">
 .....
</AnchorPane>

从启动方法中删除 MainController :

@Override
public void start(Stage primaryStage) throws Exception {

Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
// primaryStage.setFullScreen(true);
primaryStage.show();
}

现在您可以在 MainController 实例化时对其进行测试,或者您可以创建一个按钮并调用一个操作方法来测试您的方法:

public class MainController implements Initializable {
   @FXML Label answer1;
   @FXML Label answer2;

   @Override
   public void initialize(URL url, ResourceBundle rb){
       if(answer1 != null && answer2 != null){
              answer1.setText("FIRST");
              answer2.setText("SECOND");
       }
      ...
   }

   ...

}

【讨论】:

  • 您不需要在方法初始化中检查null,因为当它进入该方法时,控制器的元素已被初始化。
  • 在某些情况下,有时会多次调用该方法
  • 我不知道。如果你想举个例子。
  • 这对我来说也是新闻。来自Introduction to FXML(强调我的):“但是,当需要对控制器的行为及其管理的元素进行更多控制时,控制器可以定义一个initialize() 方法,当其关联文档的内容已完全加载时,该方法将在实现控制器上调用一次。当然您可以自己调用该方法一个在没有注入必要字段的情况下创建的控制器实例,但是......
  • 更重要的是,您不需要实现Initializable,在这种情况下,您可以将initialize() 方法设为private 并对其进行注释@FXML。然后你可以确保它不会在任何其他时间被调用。
【解决方案2】:

您创建了一个不与 fxml 文件一起使用的控制器类的实例,因此 answer1answer2 不会注入到您为其调用 testFunction 的实例中。

假设您使用fx:controller属性(see Introduction to FXML: Controllers)正确指定了fxml文件根元素中的控制器类,您可以从@987654327获取加载过程中FXMLLoader创建的控制器实例@instance,只要你使用一个:

FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/Main.fxml"));
Parent root = loader.load();
MainController m = loader.getController();
m.testFunction();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2020-04-10
    • 1970-01-01
    相关资源
    最近更新 更多