title: JavaFX How can I access Application object in Controller?
body: <p>I'm creating a <em><strong>simple</strong></em> javaFX program. In <code>HelloApplication</code> I want to do the logic, and in <code>HelloController</code> I want to listen to button clicks which then execute methods inside the <code>HelloApplcation</code>.</p>
  <p><strong>HelloApplication.java:</strong></p>
  <pre><code>Timeline timeline;
  @FXML
  HelloController HC;
  FXMLLoader fxmlLoader;
  private int counter=0;
  public void start(Stage stage) throws IOException {
          fxmlLoader = new FXMLLoader(HelloApplication.class.getResource(&quot;hello-view.fxml&quot;));
  
          Scene scene = new Scene(fxmlLoader.load(), 640, 480);
          stage.setTitle(&quot;Program!&quot;);
          stage.setScene(scene);
          stage.show();
          StartCounter(&quot;start&quot;);
  }
  public void StartCounter(String status)
      {
          HC = fxmlLoader.getController();
          if(timeline==null) {
              timeline = new Timeline(new KeyFrame(Duration.seconds(1), ev -&gt; {
                  HC.getCmd().setText(String.valueOf(counter));
                  counter++;
              }));
              timeline.setCycleCount(Animation.INDEFINITE);
          }else{
              if(status==&quot;stop&quot;)
                  timeline.stop();
              else if(status==&quot;start&quot;)
                  timeline.play();
          }
  }
  </code></pre>
  <p><strong>HelloController.java:</strong></p>
  <pre><code>  HelloApplication helloApp=new HelloApplication(); //this creates another instance...
   @FXML
      protected void onHelloButtonClick() {
          helloApp.StartCounter(&quot;stop&quot;);
      }
  </code></pre>
  <p>But how can I access <strong>HelloApplication</strong> object that was created on <code>launch()</code>?
  Something like <code>loader.getApplication()</code> would be perfect instead of creating a new object in controller (which doesn't work anyway).</p>
  <p>Right now, after mouse click the helloApp.StartCounter() causes:
  <em><strong>java.lang.reflect.InvocationTargetException</strong></em></p>