0

I tried almost every available solution but nothing seems to work I need to pass a string between 2 Javafx scenes, but when I click to navigate to the next scene nothing happens. here's my code so far:

String myVariable = "test";
FXMLLoader loader =new FXMLLoader(getClass().getResource("/fxml/Comments.fxml"));
Parent root = null;
CommentsController commentsController = new CommentsController();
commentsController = loader.getController();
commentsController.transferMessage(myVariable);
 Stage stage = new Stage();
 stage.setScene(new Scene(root));
 stage.show(); 

PS: I have transferMessage() method set-up in the second scene.

AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
  • There should be a `NullPointerException` happening. The `controller` property of the loader as well as `root` remain `null`, since you never call `load()` or assign a different value to `root`. – fabian Oct 29 '19 at 06:34

1 Answers1

3
  1. surround your chunk of code with a try{}...catch(IOException ex){}
  2. replace Parent root = null with Parent root = load.loader()

That's all it takes

Thorvald
  • 3,424
  • 6
  • 40
  • 66
  • 2
    Preferrably the initial assignment to the `commentsController` variable should also be removed to avoid creating an instance of the controller that isn't used... – fabian Oct 29 '19 at 06:36