title: Spring won't load controller classes for JavaFx views
body: <p>I'am currently working on a project in which I'm using Spring and JavaFX and I ran into a problem.
  I have initially a login frame and after the user presses Login button another frame has to appear. The problem is that after I build my second FXML file (for the second frame) and try to link it to its controller the Spring won't make this linking for some reason. I have annotated my controller class with @Component but it does make any difference. If I do not set my fx:controller (leave it empty) , the frame successfully loads. I've been spending the last 6-7 hours on this issue and I haven't found any satisfactory answer.</p>
  
  <p>My <strong>Controller</strong> class looks like:</p>
  
  <pre><code>package project.eHealth;
  
  import java.net.URL;
  import java.util.ResourceBundle;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.SpringApplication;
  import org.springframework.context.ConfigurableApplicationContext;
  import org.springframework.stereotype.Component;
  import org.springframework.stereotype.Controller;
  
  import javafx.collections.FXCollections;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.fxml.Initializable;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.TableColumn;
  import javafx.scene.control.TableView;
  import javafx.stage.Stage;
  import project.eHealth.bussiness.AppointmentDto;
  import project.eHealth.bussiness.AppointmentService;
  
  @Component
  public class DoctorViewController {
  
      @FXML
      private TableView&lt;AppointmentDto&gt; appointmentTable;
  
      @FXML
      private TableColumn&lt;AppointmentDto,String&gt; doctorNameColumn ;
  
      @FXML
      private TableColumn&lt;AppointmentDto,String&gt; patientNameColumn ;
  
  
      @Autowired
      private AppointmentService appointments;
  
      @FXML
      public void initialize(){
          patientNameColumn.setCellValueFactory(cellData -&gt; cellData.getValue().getPatientName());
      }
  
      @FXML
      public TableView&lt;AppointmentDto&gt; getAppointmentTable() {
          return appointmentTable;
      }
  
  
      public void setAppointmentTable(TableView&lt;AppointmentDto&gt; appointmentTable) {
          this.appointmentTable = appointmentTable;
      }
  
  
      @FXML
      public TableColumn&lt;AppointmentDto, String&gt; getDoctorNameColumn() {
          return doctorNameColumn;
      }
  
  
      public void setDoctorNameColumn(TableColumn&lt;AppointmentDto, String&gt; doctorNameColumn) {
          this.doctorNameColumn = doctorNameColumn;
      }
  
      @FXML
      public TableColumn&lt;AppointmentDto, String&gt; getPatientNameColumn() {
          return patientNameColumn;
      }
  
  
      public void setPatientNameColumn(TableColumn&lt;AppointmentDto, String&gt; patientNameColumn) {
          this.patientNameColumn = patientNameColumn;
      }
  
  
      @FXML
      public AppointmentService getAppointments() {
          return appointments;
      }
  
  
      public void setAppointments(AppointmentService appointments) {
          this.appointments = appointments;
      }
  
  }
  </code></pre>
  
  <p>It's corresponding FXML file looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import java.lang.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  
  &lt;AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="project.eHealth.DoctorViewController"&gt;
     &lt;children&gt;
        &lt;SplitPane dividerPositions="0.4882943143812709" layoutX="148.0" layoutY="71.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt;
          &lt;items&gt;
            &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0"&gt;
                 &lt;children&gt;
                    &lt;TableView fx:id="appointmentTable" layoutX="14.0" layoutY="60.0" prefHeight="398.0" prefWidth="289.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt;
                      &lt;columns&gt;
                        &lt;TableColumn fx:id="doctorNameColumn" prefWidth="95.0" text="Patient Name" /&gt;
                        &lt;TableColumn fx:id="patientNameColumn" prefWidth="105.0" text="Date Issued" /&gt;
                      &lt;/columns&gt;
                       &lt;columnResizePolicy&gt;
                          &lt;TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /&gt;
                       &lt;/columnResizePolicy&gt;
                    &lt;/TableView&gt;
                 &lt;/children&gt;
              &lt;/AnchorPane&gt;
            &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0"&gt;
                 &lt;children&gt;
                    &lt;Label layoutX="23.0" layoutY="21.0" text="Patient name" /&gt;
                    &lt;Label layoutX="30.0" layoutY="99.0" prefHeight="17.0" prefWidth="70.0" text="Date Issued" /&gt;
                    &lt;Button layoutX="172.0" layoutY="227.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="111.0" text="Accept" /&gt;
                    &lt;Button layoutX="172.0" layoutY="277.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="111.0" text="Reject" /&gt;
                 &lt;/children&gt;
              &lt;/AnchorPane&gt;
          &lt;/items&gt;
        &lt;/SplitPane&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>The main application class :</p>
  
  <pre><code>package project.eHealth;
  
  import java.io.IOException;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.ConfigurableApplicationContext;
  import org.springframework.stereotype.Component;
  import org.springframework.stereotype.Controller;
  
  import javafx.application.Application;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  
  
  
  @SpringBootApplication
  public class EHealthApplication extends Application {
      private ConfigurableApplicationContext context;
      private Parent rootNode;
      private Stage primary;
      private Stage doctorStage;
  
      public static void main(String[] args) {
          Application.launch(args);
      }
  
      @Override
      public void init() throws Exception {
          context = SpringApplication.run(EHealthApplication.class);
          FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Main.fxml"));
          fxmlLoader.setControllerFactory(context::getBean);
          rootNode = fxmlLoader.load();
  }
  
      @Override
      public void start(Stage primaryStage) throws Exception {
          this.primary = primaryStage;
          primaryStage.setScene(new Scene(rootNode));
          primaryStage.setTitle("E-Health Login");
          primaryStage.show();
  
  
      }
  
      @Override
      public void stop() throws Exception {
          context.close();
  }
  
      @FXML
      void showDoctorFrame() throws IOException{
          System.out.println("Handle regular User");
          FXMLLoader loader = new FXMLLoader();
          loader.setLocation(EHealthApplication.class.getResource("DoctorView.fxml"));
          loader.setControllerFactory(context::getBean);
          rootNode = loader.load();
  
          doctorStage = new Stage();
          doctorStage.setTitle("PatientView");
          Scene scene = new Scene(rootNode);
          doctorStage.setScene(scene);
          doctorStage.show();
      }
  
  }
  </code></pre>
  
  <p>The method that is executed after the login button is pressed is <em>showDoctorFrame()</em></p>
  
  <p>The stack trace is:</p>
  
  <pre><code>Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
      at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
      at javafx.event.Event.fireEvent(Event.java:198)
      at javafx.scene.Node.fireEvent(Node.java:8413)
      at javafx.scene.control.Button.fire(Button.java:185)
      at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
      at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
      at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
      at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
      at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
      at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
      at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
      at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
      at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
      at javafx.event.Event.fireEvent(Event.java:198)
      at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
      at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
      at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
      at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
      at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
      at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
      at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
      at com.sun.glass.ui.View.notifyMouse(View.java:937)
      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)
  Caused by: java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
      at sun.reflect.GeneratedMethodAccessor15.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
      at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
      ... 48 more
  Caused by: javafx.fxml.LoadException: 
  /C:/Users/Dragos/Desktop/eHealth/target/classes/project/eHealth/DoctorView.fxml:8
  
      at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
      at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
      at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
      at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
      at project.eHealth.EHealthApplication.showDoctorFrame(EHealthApplication.java:60)
      ... 58 more
  Caused by: java.lang.NullPointerException
      at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:929)
      at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
      at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
      at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
      at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
      at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
      ... 61 more
  </code></pre>
  
  <p>Any kind of help is deeply appreciated!</p>