title: How can a textfield from fxml file be updated by setText in java file?
body: <p>I am looking to update text in a textfield based on some value. In order to make this sample simpler I have made my program smaller. The problem seems to be when I put top.setText("This is my new Text");</p>
  
  <p>I looked at this:
  <a href="https://stackoverflow.com/questions/10601648/how-to-change-the-text-of-textfield-in-java-fx-2">how to change the text of TextField in java fx 2</a></p>
  
  <p>but the answer does not seem to make sense. I don't know why you'd initialize a textfield that has already been implemented. Regardless it did not work.</p>
  
  <p>I have also looked at:
  <a href="https://stackoverflow.com/questions/22145327/nullpointerexception-javafx-label-settext">NullPointerException (JavaFX Label.setText())</a></p>
  
  <p>This seems to be the closest to what I think is the issue, but when I did the following I get an error. Just for clarity this is in the JavaFXApplication5 class.</p>
  
  <pre><code>try {
          FXMLLoader loader = new FXMLLoader(
              getClass().getResource("FXML.fxml")
          );
          FXMLLoader.setController(this); // non-static method setController(Object) 
                                          // can not be referenced from static context ERROR****
          Parent root = (Parent) loader.load();
          /*Parent root = FXMLLoader.load(getClass().getResource("FXML.fxml"));
  
          Scene scene = new Scene(root);
          stage.setScene(scene);
          stage.setTitle("java code");
          stage.show();
          */
  
      }
  </code></pre>
  
  <p>From reading on the internet I wondered if there was a race condition: <a href="http://gotoanswer.stanford.edu/nullpointerexception_in_javafx_initialize_method-8807679/" rel="nofollow noreferrer">http://gotoanswer.stanford.edu/nullpointerexception_in_javafx_initialize_method-8807679/</a></p>
  
  <p>So I tried:</p>
  
  <pre><code>Platform.runLater(new Runnable() {
    @Override public void run() {
      top.setText("This is my new Text");
    }
  });
  </code></pre>
  
  <p>But that did not work. I know that it can be set in Scene Builder but I need a way to dynamically change it based on values from another class. I can figure out how to do that part if I can just figure out how to set it to begin with. Hopefully this explains enough to get some help.</p>
  
  <pre><code>    FXMLController Class:
      public class FXMLController implements Initializable {
  
          @FXML private TextField top;
  
          public FXMLController() {
              System.out.println("Hi");
              top.setText("This is my new Text"); // This breaks the program *********
          }
          @Override
          public void initialize(URL url, ResourceBundle rb) {
          } 
      }
  </code></pre>
  
  <blockquote>
    <p></p>
  </blockquote>
  
  <pre><code>    FXML.fxml class:
      &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
      &lt;?import java.lang.*?&gt;
      &lt;?import java.util.*?&gt;
      &lt;?import javafx.scene.*?&gt;
      &lt;?import javafx.scene.control.*?&gt;
      &lt;?import javafx.scene.layout.*?&gt;
  
      &lt;AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="javafxapplication5.FXMLController"&gt;
         &lt;children&gt;
            &lt;TextField fx:id="top" layoutX="171.0" layoutY="68.0" /&gt;
         &lt;/children&gt;
      &lt;/AnchorPane&gt;
  </code></pre>
  
  <blockquote>
    <p></p>
  </blockquote>
  
  <pre><code>JavaFXApplication5 class: // main class
  public class JavaFXApplication5 extends Application {
  
      @Override
      public void start(Stage stage) throws Exception {
          try {
              Parent root = FXMLLoader.load(getClass().getResource("FXML.fxml"));
              Scene scene = new Scene(root);
              stage.setScene(scene);
              stage.setTitle("java code");
              stage.show();
          }
          catch (Exception ex) {
              Logger.getLogger(JavaFXApplication5.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
      public static void main(String[] args) {
          launch(args);
      }  
  }
  </code></pre>