我的FXML代码有问题。
我想在初始化时获取舞台标题的字符串,但是当我尝试调用操作stage.getTitle()
时,FXMLLoader会抛出异常。
我也尝试在其他方法中获得标题,但它只在其中一些方法中起作用。有人能告诉我那里的问题是什么吗?
这是舞台初始化的地方:
@FXML
private void handleModellAction(ActionEvent event) throws IOException{
FXMLLoader load = new FXMLLoader(getClass().getResource("InEX.fxml"));
Parent root = (Parent) load.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
link = (Hyperlink) event.getTarget();
model = link.getText();
stage.setTitle(model);
}
这是控制器类中的initialize方法:
public void getTitle(){
System.out.println(this.stage.getTitle());
}
@Override
public void initialize(URL url, ResourceBundle rb) {
image = new Image("images/test.png", 1500, 900, true, true);
interieurImg.setImage(image);
model = this.stage.getTitle();
}
这是抛出的异常:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
... 52 more
Caused by: javafx.fxml.LoadException: file:/C:/Users/p356545/Documents/NetBeansProjects/VFAfxml3/dist/run1293777989/VFAfxml3.jar!/vfafxml3/Interieur.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at ...
at vfafxml3.InEXController.handleInterieurAction(InEXController.java:58) ... 62 more
Caused by: java.lang.NullPointerException
at vfafxml3.InterieurController.getTitle(InterieurController.java:49)
at vfafxml3.InterieurController.initialize(InterieurController.java:88)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) ... 70 more
答案 0 :(得分:2)
在initialize-method中获取舞台标题还为时过早。在使用以下实现初始化阶段后,您可以使用任何其他方法获取它:
public String getTitle(){
Stage stages = (Stage) anyChoosenElement.getScene().getWindow();
String modelString = stages.getTitle();
return modelString;
}