title: All JavaFX FXML objects null in controller
body: <p>I realise that this question has been asked before but none of the solutions have worked for me. I am starting a thread from my controller, and from there the thread gets some data from my database. The thread sends the data to my interface which is implemented in the controller. I get a null pointer exception when trying to access any of my JavaFX elements from there.</p>
  
  <p>Here is my controller:</p>
  
  <pre><code>public class SettingsPage implements PrizeReceiver{
  
      @FXML
      AnchorPane settingsAnchor;
      @FXML
      ListView&lt;String&gt; prizeList;
      @FXML
      TextField prizeField;
      @FXML
      Button load;
  
      void init(AnchorPane rootPane){
  
         BasicConfigurator.configure();
  
          AnchorPane root = null;
  
          try {
  
              File file = new File(System.getProperty("user.dir") + "/src/main/res/settings.fxml");
  
          root = FXMLLoader.load(file.toURI().toURL());
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      if (root != null) {
          rootPane.getChildren().setAll(root);
      }
  
      initPrizeList();
  
  }
  
  private void initPrizeList() {
  
      GetPrizesThread getPrizesThread = new GetPrizesThread(Database.firebaseDatabase, this);
      getPrizesThread.getThread().start();
  
  }
  
  @FXML
  void addPrize(){
  
      Database.createPrize(prizeField.getText());
  
  }
  
  @Override
  public void receivePrizes(ArrayList&lt;String&gt; prizes) {
  
      ObservableList&lt;String&gt; items = FXCollections.observableArrayList();
      items.addAll(prizes);
  
      //Iv'e tried this if else with every fxml object and always prints null
      if(prizeField==null){
          System.out.println("NULL");
      }else {
          prizeList.getItems().addAll(items);
      }
  
  }
  }
  </code></pre>
  
  <p>Here is the Thread:</p>
  
  <pre><code>public class GetPrizesThread implements Runnable {
  
  private boolean finished = false;
  
  private final Thread thread;
  private FirebaseDatabase firebaseDatabase;
  private PrizeReceiver prizeReceiver;
  
  GetPrizesThread(FirebaseDatabase firebaseDatabase, PrizeReceiver prizeReceiver){
      this.firebaseDatabase = firebaseDatabase;
      this.prizeReceiver = prizeReceiver;
      thread = new Thread(this);
  }
  
  @Override
  public void run() {
  
      ArrayList&lt;String&gt; prizes = new ArrayList&lt;&gt;();
  
      DatabaseReference databaseReference = firebaseDatabase.getReference("prizes");
  
      databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot snapshot) {
  
              HashMap map = (HashMap) snapshot.getValue();
  
              Set keySet = map.keySet();
  
              ArrayList&lt;String&gt; listOfKeys = new ArrayList&lt;String&gt;(keySet);
  
              prizes.clear();
              prizes.addAll(listOfKeys);
  
              finished = true;
  
          }
  
          @Override
          public void onCancelled(DatabaseError error) {
              finished = true;
          }
      });
  
      while(!finished) {
          try {
              Thread.sleep(1);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
      }
  
      prizeReceiver.receivePrizes(prizes);
  
  }
  
  Thread getThread() {
      return thread;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the FXML file(built with scene builder):</p>
  
  <pre><code>&lt;AnchorPane fx:id="settingsAnchor" prefHeight="454.0" prefWidth="700.0" 
  xmlns="http://javafx.com/javafx/8.0.161" xmlns:fx="http://javafx.com/fxml/1" 
  fx:controller="SettingsPage"&gt;
     &lt;children&gt;
    &lt;GridPane layoutX="-2.0" layoutY="-1.0" prefHeight="400.0" prefWidth="600.0"&gt;
      &lt;columnConstraints&gt;
        &lt;ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /&gt;
        &lt;ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /&gt;
      &lt;/columnConstraints&gt;
      &lt;rowConstraints&gt;
        &lt;RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /&gt;
        &lt;RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /&gt;
        &lt;RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /&gt;
      &lt;/rowConstraints&gt;
       &lt;children&gt;
  
          &lt;ListView fx:id="prizeList" prefHeight="400.0" prefWidth="246.0" GridPane.rowSpan="3"&gt;
             &lt;GridPane.margin&gt;
                &lt;Insets bottom="8.0" left="8.0" right="8.0" top="8.0" /&gt;
             &lt;/GridPane.margin&gt;
          &lt;/ListView&gt;
  
          &lt;Button mnemonicParsing="false" onAction="#addPrize" text="Add Prize" GridPane.columnIndex="1" GridPane.rowIndex="2"&gt;
             &lt;GridPane.margin&gt;
                &lt;Insets left="8.0" top="40.0" /&gt;
             &lt;/GridPane.margin&gt;
          &lt;/Button&gt;
          &lt;TextField fx:id="prizeField" promptText="Prize Name" GridPane.columnIndex="1" GridPane.rowIndex="2"&gt;
             &lt;GridPane.margin&gt;
                &lt;Insets bottom="15.0" left="8.0" right="8.0" /&gt;
             &lt;/GridPane.margin&gt;
          &lt;/TextField&gt;
       &lt;/children&gt;
    &lt;/GridPane&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>When I don't have the if/else in place this is my error:</p>
  
  <pre><code>Exception in thread "Thread-5" java.lang.NullPointerException
  at SettingsPage.receivePrizes(SettingsPage.java:74)
  at GetPrizesThread.run(GetPrizesThread.java:59)
  at java.lang.Thread.run(Thread.java:748)
  </code></pre>
  
  <p>line 74 is </p>
  
  <blockquote>
    <p>prizeList.getItems().addAll(items);</p>
  </blockquote>
  
  <p>Thanks for any help.</p>