0

I have a project where I am creating the GUI on Scene Builder. When I first create all the elements of my GUI, it works perfectly. But as soon as I add ids and onActions to my textFields and other elements, it breaks the code saying that there is a "Exception in Application start method". I am not sure how to deal with this, since I have been doing the exact same thing with other projects and this never happened before.

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <ListView fx:id="eventListView" layoutX="239.0" layoutY="60.0" prefHeight="380.0" prefWidth="348.0">
         <contextMenu>
            <ContextMenu>
              <items>
                <MenuItem mnemonicParsing="false" text="Details" />
                  <MenuItem mnemonicParsing="false" text="Remove" />
              </items>
            </ContextMenu>
         </contextMenu>
      </ListView>
      <TextField fx:id="titleTextField" layoutX="46.0" layoutY="101.0" />
      <TextField fx:id="descriptionTextField" layoutX="46.0" layoutY="166.0" />
      <DatePicker fx:id="datePickerField" layoutX="40.0" layoutY="238.0" />
      <ComboBox fx:id="cmbAddType" layoutX="47.0" layoutY="295.0" onAction="#selectedType" prefWidth="150.0" />
      <Button fx:id="addEventButton" layoutX="72.0" layoutY="365.0" mnemonicParsing="false" onAction="#handleAddEvent" prefHeight="30.0" prefWidth="100.0" text="Add" />
      <ComboBox fx:id="cmbOrderFilter" layoutX="614.0" layoutY="153.0" onAction="#orderList" prefWidth="150.0" />
      <ComboBox fx:id="cmbTypeFilter" layoutX="614.0" layoutY="208.0" onAction="#filterType" prefWidth="150.0" />
      <Button fx:id="removeButton" layoutX="648.0" layoutY="257.0" mnemonicParsing="false" onAction="#handleRemoveEvent" prefHeight="30.0" prefWidth="100.0" text="Remove" />
      <Label layoutX="109.0" layoutY="74.0" text="Title" />
      <Label layoutX="88.0" layoutY="148.0" text="Description" />
      <Label layoutX="108.0" layoutY="213.0" text="Date" />
      <Label layoutX="108.0" layoutY="272.0" text="Type" />
      <Label layoutX="673.0" layoutY="127.0" text="Order" />
      <Label layoutX="675.0" layoutY="183.0" text="Type" />
      <Label layoutX="381.0" layoutY="25.0" text="Events List" />
   </children>
</AnchorPane>
public class MainWindow {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Button addEventButton;

    @FXML
    private ComboBox<?> cmbAddType;

    @FXML
    private ComboBox<?> cmbOrderFilter;

    @FXML
    private ComboBox<?> cmbTypeFilter;

    @FXML
    private DatePicker datePickerField;

    @FXML
    private TextField descriptionTextField;

    @FXML
    private ListView<?> eventListView;

    @FXML
    private Button removeButton;

    @FXML
    private TextField titleTextField;

    @FXML
    void filterType(ActionEvent event) {

    }

    @FXML
    void handleAddEvent(ActionEvent event) {

    }

    @FXML
    void handleRemoveEvent(ActionEvent event) {

    }

    @FXML
    void orderList(ActionEvent event) {

    }

    @FXML
    void initialize() {
        assert this.addEventButton != null : "fx:id=\"addEventButton\" was not injected: check your FXML file 'MainWindow.fxml'.";
        assert this.cmbAddType != null : "fx:id=\"cmbAddType\" was not injected: check your FXML file 'MainWindow.fxml'.";
        assert this.cmbOrderFilter != null : "fx:id=\"cmbOrderFilter\" was not injected: check your FXML file 'MainWindow.fxml'.";
        assert this.cmbTypeFilter != null : "fx:id=\"cmbTypeFilter\" was not injected: check your FXML file 'MainWindow.fxml'.";
        assert this.datePickerField != null : "fx:id=\"datePickerField\" was not injected: check your FXML file 'MainWindow.fxml'.";
        assert this.descriptionTextField != null : "fx:id=\"descriptionTextField\" was not injected: check your FXML file 'MainWindow.fxml'.";
        assert this.eventListView != null : "fx:id=\"eventListView\" was not injected: check your FXML file 'MainWindow.fxml'.";
        assert this.removeButton != null : "fx:id=\"removeButton\" was not injected: check your FXML file 'MainWindow.fxml'.";
        assert this.titleTextField != null : "fx:id=\"titleTextField\" was not injected: check your FXML file 'MainWindow.fxml'.";

    }

}
public class Main extends Application {
    private static final String WINDOW_TITLE = "Bulletin Board";
    private static final String GUI_RESOURCE = "view/MainWindow.fxml";

    /**
     * JavaFX entry point.
     *
     * @precondition none
     * @postcondition none
     *
     * @throws IOException
     */
    @Override
    public void start(Stage primaryStage) throws IOException {
        Parent parent = FXMLLoader.load(getClass().getResource(Main.GUI_RESOURCE));
        Scene scene = new Scene(parent);
        primaryStage.setTitle(WINDOW_TITLE);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * Primary Java entry point.
     *
     * @precondition none
     * @postcondition none
     *
     * @param args command line arguments
     */

    public static void main(String[] args) {
        Main.launch(args);
    }

}

Exception in Application start method java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465) at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1082) Caused by: java.lang.RuntimeException: Exception in Application start method at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901) at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196) at java.base/java.lang.Thread.run(Thread.java:833) Caused by: javafx.fxml.LoadException: No controller specified. /C:/Users/vvhs1/OneDrive/Documents/UWG/2022/Fall%202022/CS1302/Project3/BulletinBoardRepo/BulletinBoard/bin/edu/westga/cs1302/bulletin_board/view/MainWindow.fxml:28

at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2703) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:568) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:781) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2924) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2639) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203) at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196) at edu.westga.cs1302.bulletin_board.Main.start(Main.java:30) at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847) at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484) at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456) at javafx.graphics@18.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184) ... 1 more Exception running application edu.westga.cs1302.bulletin_board.Main

I have tried changing the JavaFX version, reinstall Scene Builder, and did not work. Every time that I delete all the components on my FXML file and rebuild it, it works until I add the ids and onAction. As I mention before, I have been working with scene builder and doing this for the past months and this never happened.

  • 2
    It is easier to read stack traces formatted as code rather than quoted. – jewelsea Nov 24 '22 at 19:09
  • Read https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors for valuable advice on reading and interpreting a stack trace. I strongly recommend you try to understand what is wrong from the stack trace before posting here. – James_D Nov 25 '22 at 02:54

1 Answers1

3

The error message tells you what is wrong:

Caused by: javafx.fxml.LoadException: No controller specified.

You need to associate a controller with the FXML (if you provide instructions in the FXML which use a controller, e.g., setting action handlers in FXML).

See the FXML documentation on controllers.

You can specify the controller in FXML by providing a reference in the root element (substituting the controller name with the fully qualified name of your controller):

fx:controller="com.foo.MyController"

SceneBuilder can assist in setting the controller:

Or you can set the controller on the loader.

FXMLLoader loader = new FXMLLoader();
loader.setController(
    new MyController()
);

Or you can use a controller factory, which is usually used in conjunction with dependency injection frameworks, such as Spring.

jewelsea
  • 150,031
  • 14
  • 366
  • 406