JavaFX - 尝试添加函数以启动函数时出错

时间:2016-06-25 00:00:23

标签: java javafx nullpointerexception runtimeexception

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

主题:我的班级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");
    }
}

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

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

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

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您的MainController应该实现Initializable,您应该在fxml中设置控制器:  e.g。

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

   ...

}

答案 1 :(得分:0)

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

假设您使用fx:controller属性(see Introduction to FXML: Controllers)在fxml文件的根元素中正确指定了控制器类,您可以获取FXMLLoader期间由FXMLLoader创建的控制器实例。从FXMLLoader loader = new FXMLLoader(getClass().getResource("/application/Main.fxml")); Parent root = loader.load(); MainController m = loader.getController(); m.testFunction(); 实例加载进程,前提是您使用了一个:

<td class="Normal">street name1<br>street name 2<br>city, state zipcode<br>country<br>contact no</TD>