简体   繁体   中英

fxml getTitle method can't be interpreted by FXMLLoader

I have a problem with my FXML code. I want to get the String of the stage title when I initialize it, but when I try to call the operation stage.getTitle() the FXMLLoader throws an exception. I also tried to get the title within other methods but it worked only in some of them. Can someone tell me what the problem is right there?

This is where the stage is initialized:

@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);
}

This is the initialize method in the controller class:

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();
}

This is the exception that is thrown:

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

It is too early to get the stage title in the initialize-method. You can get it in any other method after the stage is initialized with the following implementation:

public String getTitle(){
    Stage stages = (Stage) anyChoosenElement.getScene().getWindow();
    String modelString = stages.getTitle();
    return modelString;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM