【发布时间】:2016-06-27 16:43:05
【问题描述】:
我有一个名为 MainStage 的类,它扩展到 javafx 的舞台类。 这是该类的一部分。
public void changeScene(){
if(onLogin)
setScene(mainScene);
else{
setScene(loginScene);
onLogin = false;
}
}
我在 MainStage 类中使用此方法来更改场景。我在控制器内部为场景调用 MainStage
public class loginSceneController{
@FXML
private Button submit;
@FXML
private TextField usernameField;
@FXML
private PasswordField passwordField;
MainStage stage = (MainStage) submit.getScene().getWindow(); //This is where the nullpointer is thrown
public void handle() {
submit.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
stage.changeScene();
System.out.println("Stage changed sucessfully!!");
}
});
}
}
当我尝试运行它时,它会抛出这个:
Exception in thread "main" 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$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: /C:/Users/Max/workspace/CloudCCP/target/classes/Window/LoginScene.fxml:9
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at Window.MainStage.<init>(MainStage.java:24)
at Window.Window.start(Window.java:28)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.NullPointerException
at Window.loginSceneController.<init>(loginSceneController.java:30)
【问题讨论】:
-
我该如何解决这个问题?
-
MainStage stage声明不在任何方法中,因此它被视为类的字段。这意味着它在 loginSceneController 实例被创建并可供其他代码使用之前运行。由于还没有其他代码可以看到该对象,因此所有其他字段(包括submit)仍然为空。 -
VGR 的评论回答了这个问题。你能解释一下java运行程序的顺序吗?
-
此外,即使您将分配移至
initialize方法,也不会设置场景,因为显然您需要在将 fxml 添加到场景之前加载它。只需在EventHandler中登台即可。这些操作并不昂贵......
标签: java user-interface javafx