title: How can I modify data from another controller class?
body: <p>I have a main class (Main.java), two FXML files (FXML1.fxml, FXML2.fxml) and the corresponding controller (FXML1Controller.java, FXML2Controller.java).</p>
  
  <p>In the FXML1.fxml I have two text fields and two buttons.
  In the associated controller (FXML1Controller.java) I have declared the text fields and the button as follows:</p>
  
  <pre><code>public TextField textField1;
  public TextField textField2;
  
  public Button buttonchange;
  public Button buttonOpenWindow;
  
  public void open ()
  {
  ...
       stage.show ();
  
  }
  public void change ()
  {
      textField2.setText (textField1.getText ());
  }
  </code></pre>
  
  <p>I type text into textField1. When I click on the buttonChange, then the text should be set in textfield2. Works. It's simple.
  When I click the buttonOpen, then a new window opens.
  There, I have only one button, but want to do the same.
  So something in the way like this:</p>
  
  <pre><code>public void changeFromHere ()
  {
        FXMLController1 c1 = new FXMLController1 ();
      c1.change ();
  }
  </code></pre>
  
  <p>I also know that similar questions have been asked here.
  But somehow it does not work the way I want it.</p>
  
  <p>I always get a NullPointerException. For sure. I know.
  So I have done the following:</p>
  
  <pre><code>import java.io.IOException;
  import java.net.URL;
  import java.util.ResourceBundle;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javafx.application.Platform;
  import javafx.event.ActionEvent;
  import javafx.fxml.FXMLLoader;
  import javafx.fxml.Initializable;
  import javafx.scene.control.Button;
  import javafx.scene.layout.Pane;
  import javafx.stage.Stage;
  ...
  public class FXML2Controller implements Initializable
  {
  
      ...
  
  
  
       public Button buttonChangeFromHere;
  
  
       URL location;
       FXMLLoader fxmlLoader;
       Pane root;
       FXMLController1 fc1;
  
  
       public voidChangeFromHere ()
       {
           fc1.change ();
       }
       @Override
       public void initialize (URL url, ResourceBundle rb)
       {
           try
           {
               location = getClass ().getResource ("FXML1.fxml");
               fxmlLoader = new FXMLLoader (location);
               root = (Pane) fxmlLoader.load ();
               fc1 = (FXMLController1) fxmlLoader.getController ();
           } catch (IOException ex)
           {
               Logger.getLogger (FXML2Controller.class.getName ()) .log (Level.SEVERE, null, ex);
           }
       }
  ...
  }
  </code></pre>
  
  <p>Now I can not get a NullPointerException and it should work this way. But nothing happened !?</p>
  
  <p>If I use for testing System.out.println (fc1.textField2.getText ());, then I get the text "Hello", which I have defined in JavaFXSceneBuilder.
  If I remove the text in SceneBuilder, then I get "null". Sure. Likewise, I get "null" when I type in the term "Hello" (in textField1) while program is running.
  I have also tried to initialize the textfields first in the initialize method. Then I also get always the text "Hello", although I typed "Byebye".</p>
  
  <p>The solutions to similar questions in this forum can not help me.
  That's why I put so a similar question again.
  This code is also just an example. I can not paste my whole code here. What I really want to do is:
  In my program I have a list (listView).
  In the list are paths to files that I save as a file. Works fine (In the Main Controller, Main.fxml). There, I have a method public void save (). Now I want to call from another controller class's the save () method. But almost all variables are "null". 
  But they can't be "null", because the listView shows me the entrys. 
  I do not think that getter and setter methods are appropriate, because it would be very much redundant code. And I don't want that. 
  If someone could answer the question how to do it with textfields from example, then I would be grateful.
  If it does not work with textfields, it will not work with other components, too:-(.
  Thanks in advance.</p>