title: How to access JavaFX components in the constructor of Controller Class
body: <p>I am loading an FXML file the following way</p>
  
  <pre><code>        FXMLLoader loader = new FXMLLoader(getClass().getResource("register.fxml"));
          Controller c = new Controller("Welcome to EAST");
          loader.setController(c);
          Scene scene = new Scene(loader.load());
          fx.setScene(scene); // fx is a JFXPanel which is used to integrate FX in Swing
  </code></pre>
  
  <p>The above code is working fine, now I want to access the the default values of <code>textFields</code> of the <code>register.fxml</code> in the constructor of Controller Class but It gives and error: <strong>Exception in thread "JavaFX Application Thread" java.lang.NullPointerException</strong>. </p>
  
  <p><strong>Note:</strong> I have assigned some default values to the textFields that is why I want to access them. </p>
  
  <p>following is My Controller class</p>
  
  <p>Controller.java</p>
  
  <pre><code>public class Controller {
  
      @FXML
      private TextField username;
      @FXML
      private PasswordField pass;
      @FXML
      private TextField ip;
      @FXML
      private TextField sPort;
      @FXML
      private TextField fPort;
  
      public Controller(String str) {
          System.out.println(str);
          System.out.println(ip.getText()); // this line gives the NullPointerException Error
      }
  
      @FXML
      private void login() {
  
      }
  
      @FXML
      private void register(){
  
      }
  
      @FXML
      private void cancel(){
          System.exit(0);
      }
  }
  </code></pre>
  
  <p>Any suggestions will be greatly appreciated . Thanks :)</p>