title: JavaFX accessing controller gives nullpointerexception
body: <p>What i'm trying to achieve is getting a controller instance from the main method, so i can call methods of the controller from another class and update the fxml. Anyways here's my code:</p>
  
  <p>Main class:</p>
  
  <pre><code>public class Main extends Application {
  
  Controller controller;
  @Override
  public void start(Stage primaryStage) throws Exception{
  
      FXMLLoader fxmlLoader = new FXMLLoader();
  
      Parent root = fxmlLoader.load(getClass().getResource("sample.fxml"));
      controller = fxmlLoader.getController();
  
      primaryStage.setTitle("uPick Smart Service");
      primaryStage.setScene(new Scene(root, 1600, 600));
  
      primaryStage.show();
  
      ConnectionHandling connectionHandling = new ConnectionHandling();
      Thread X = new Thread (connectionHandling);
      X.start();
  
  }
  
  public static void main(String args[]){
      launch(args);
  
  }
  
  public Controller getController(){
      return controller;
  }
  
  
  }
  </code></pre>
  
  <p>My controller class:</p>
  
  <pre><code>public class Controller {
  
  public HBox billbox;
  
  public int childnr;
  
  
  public void createBill() {
      System.out.println("Creating");
      TableView&lt;Item&gt; bill = new TableView&lt;&gt;();
  
      DropShadow dropShadow = new DropShadow();
      dropShadow.setRadius(5.0);
      dropShadow.setOffsetX(3.0);
      dropShadow.setOffsetY(3.0);
      dropShadow.setColor(Color.color(0.4, 0.5, 0.5));
  
      VBox fullbill = new VBox();
      fullbill.setPadding(new Insets(1, 1, 1, 1));
      fullbill.getStyleClass().add("fullbill");
  
      TableColumn&lt;Item, String&gt; nameColumn = new TableColumn&lt;&gt;("Emri");
      nameColumn.setMinWidth(200);
      nameColumn.setCellValueFactory(new PropertyValueFactory&lt;&gt;("name"));
  
      TableColumn&lt;Item, String&gt; quantsColumn = new TableColumn&lt;&gt;("Sasia");
      quantsColumn.setMinWidth(50);
      quantsColumn.setCellValueFactory(new PropertyValueFactory&lt;&gt;("quants"));
  
      double tablewidth = nameColumn.getWidth() + quantsColumn.getWidth();
  
      Label tablenrlabel = new Label("Table 5");
      tablenrlabel.getStyleClass().add("tablenr-label");
      tablenrlabel.setMinWidth(tablewidth);
  
      Button closebutton = new Button("Mbyll");
      closebutton.setMinWidth(tablewidth);
      closebutton.getStyleClass().add("red-tint");
  
      bill.setItems(getItem());
      bill.setMinWidth(256);
      bill.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
      bill.getColumns().addAll(nameColumn, quantsColumn);
  
      fullbill.setEffect(dropShadow);
      fullbill.getChildren().addAll(tablenrlabel, bill, closebutton);
  
      billbox.getChildren().addAll(fullbill);
  
      childnr += 1;
  
      //Loops over every button in every vbox and gives it a seperate listener (the index of the button is hardcoded so it can cause problems if you add more items)
      for (int i = 0; i &lt; childnr; i++) {
          VBox box = (VBox) billbox.getChildren().get(i);
          Button btn = (Button) box.getChildren().get(2); //if sudden issues change this
          btn.setId(Integer.valueOf(i).toString());
          btn.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
              @Override
              public void handle(ActionEvent event) {
                  int index = billbox.getChildren().indexOf(btn.getParent());
                  billbox.getChildren().remove(index);
                  childnr -= 1;
                  System.out.println(btn.getId());
              }
          });
      }
      System.out.println("done");
  }
  
  }
  </code></pre>
  
  <p>And the class trying to call controller method:</p>
  
  <pre><code>public class TakeOrder implements Runnable {
  Socket SOCK;
  Controller controller;
  
  //OrderIndexes: "Order","Waiter","Payment"
  
  private int NonConnectedDatas = 2;
  
  public TakeOrder(Socket X){
      this.SOCK = X;
  }
  
  public void CheckConnection() throws IOException{
      System.out.println("Checking connection");
      if(!SOCK.isConnected()){
          System.out.println("Dissconectiong");
          for(int i = 0; i &lt; ConnectionHandling.ConnectionArray.size(); i++){
              if(ConnectionHandling.ConnectionArray.get(i) == SOCK){
                  ConnectionHandling.ConnectionArray.remove(i);
              }
          }
      }
  }
  
  public void run(){
      try{
          try{
              CheckConnection();
  
  
              ObjectInputStream ob = new ObjectInputStream(SOCK.getInputStream());
  
              String[] structuredArray = (String[])ob.readObject();
  
              String tablenr = structuredArray[0];
              String index = structuredArray[1];
  
              ArrayList&lt;String&gt; names = new ArrayList&lt;&gt;();
              ArrayList&lt;String&gt; quants = new ArrayList&lt;&gt;();
  
              int a = 0;
              int b = 0;
              switch (index) {
                  case "Order":
                      for (int i = NonConnectedDatas; i &lt; structuredArray.length; i++) {
                          if (i % 2 == 0) {
                              names.add(a, structuredArray[i]);
                              System.out.println(names.get(a));
                              a++;
                          } else {
                              quants.add(b, structuredArray[i]);
                              System.out.println(quants.get(b));
                              b++;
                          }
                      }
                      break;
              }
  
              Platform.runLater(new Runnable() {
                  @Override
                  public void run() {
  
                  }
              });
          }finally{
              SOCK.close();
          }
      }catch(Exception X){
          System.out.print(X);
      }
  }
  
  
  
  }
  </code></pre>
  
  <p>And here's my error message:</p>
  
  <pre><code>Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
  at sample.TakeOrder$1.run(TakeOrder.java:80)
  at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
  at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
  at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>The ConnectionHandling class:</p>
  
  <pre><code>public class ConnectionHandling implements Runnable{
  
  public static ArrayList&lt;Socket&gt; ConnectionArray = new ArrayList&lt;Socket&gt;();
  
  
  public void run(){
      System.out.println("Starting");
      try{
          final int PORT = 60123;
          ServerSocket SERVER = new ServerSocket(PORT);
          System.out.println("Waiting for clients");
          while(true){
              Socket SOCK = SERVER.accept();
              ConnectionArray.add(SOCK);
  
              TakeOrder ORDER = new TakeOrder(SOCK);
              Thread X = new Thread(ORDER);
              X.setDaemon(true);
              X.start();
          }
      }catch(Exception x){
          System.out.print(x);
      }
  
  }
  </code></pre>