title: JavaFX error "null pointer Exception" when passing parameter from one Controller to another Controller
body: <p>i have tried all solution in youtube and google but none of them work. The problem is when i want to switch to new scene along sending data to another scene i got error saying</p>
  
  <blockquote>
    <p>Null pointer Exception</p>
  </blockquote>
  
  <pre><code>public class FirstController implements Initializable {
  
      @FXML private TextField textField;
      @FXML private Button btn;
      @Override
      public void initialize(URL arg0, ResourceBundle arg1) {
          // TODO Auto-generated method stub
      }
  
      public void  onClick(ActionEvent event){
          //switching to new scene
          FXMLLoader loader=new FXMLLoader();
          loader.setLocation(getClass().getResource("/application/Second.fxml"));
          try{
              loader.load();
          }catch(Exception e){ }
          SecondController sn=loader.getController();
          sn.setText(textField.getText());
          Parent p=loader.getRoot();  
          Stage window=new Stage();
          window.setScene(new Scene(p));
          window.setTitle("dfd");
          window.show();
      }
  }
  </code></pre>
  
  <p>main.fxml:</p>
  
  <pre><code>&lt;AnchorPane prefHeight="450.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Main"&gt;
     &lt;children&gt;
        &lt;Button fx:id="btn" layoutX="285.0" layoutY="264.0" mnemonicParsing="false" onAction="#onClick" text="Button" /&gt;
        &lt;TextField fx:id="textField" layoutX="237.0" layoutY="213.0" /&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>Second controller : </p>
  
  <pre><code>public class SecondController implements Initializable {
  
      @FXML private Label labelField;
      @Override
      public void initialize(URL arg0, ResourceBundle arg1) {
          // TODO Auto-generated method stub  
      }
  
      public void setText(String name){
          this.labelField.setText(name);
      } 
  }
  </code></pre>
  
  <p>Second.fxml:</p>
  
  <pre><code>`&lt;AnchorPane prefHeight="450.0" prefWidth="650.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1"&gt;
     &lt;children&gt;
        &lt;Label fx:id="labelField" layoutX="297.0" layoutY="208.0" text="Label" /&gt;
     &lt;/children&gt;
  
      &lt;/AnchorPane&gt;
  </code></pre>