0

I'm new to Java. If there are some stupid errors don't hate).
Every time I press the button to start a new Scene it gives me an error. I have double checked if the imports are right(sometimes Intellij imports wrong libraries with same name), checked if all the controllers are assigned to each fxml file, checked the path but nothing has helped.
I tried even to change the ActionEvent to MouseEvent also nothing.
I'm working now only at the Student List scene.
DashBoard Controller:

public class DashboardController implements Initializable
{
  @FXML
  private Button studentListBtn;
  @FXML
  private Button manageStudentsBtn;
  @FXML
  private Button manageClassroomBtn;

  @Override
  public void initialize(URL location, ResourceBundle resources){
    Timenow();
  }

  @FXML private void Close_Clicked(ActionEvent t){
    stop = true;
    System.exit(1);
  }

  @FXML
  private void handleButtonClick(ActionEvent mouseEvent){
    if(mouseEvent.getSource() == studentListBtn)
    {
     LoadStage("GUI/studentList.fxml");
    }
    else if(mouseEvent.getSource() == manageClassroomBtn)
    {
      LoadStage("GUI/studentList.fxml");
    }
    else if(mouseEvent.getSource() == manageClassroomBtn)
    {
      LoadStage("GUI/studentList.fxml");
    }
  }

  private void LoadStage(String fxml){
    try
    {
      Parent root = FXMLLoader.load(getClass().getResource(fxml));
      Stage stage = new Stage();
      stage.setScene(new Scene(root));
      stage.show();
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}


JavaFXML:

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="800.0" style="-fx-background-color: #FFF;" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="GUI.DashboardController">
   <children>
      <Pane layoutY="28.0" prefHeight="116.0" prefWidth="800.0" style="-fx-background-color: #5f79e6;">
         <children>
            <Label layoutX="38.0" layoutY="14.0" prefHeight="50.0" prefWidth="340.0" text="Schedule System" textFill="WHITE">
               <font>
                  <Font size="39.0" />
               </font>
            </Label>
            <Label layoutX="38.0" layoutY="68.0" prefHeight="34.0" prefWidth="226.0" text="Created by Runtime Terror" textFill="WHITE">
               <font>
                  <Font size="16.0" />
               </font>
            </Label>
            <Label fx:id="timeDashboard" layoutX="614.0" layoutY="57.0" prefHeight="56.0" prefWidth="179.0" textFill="WHITE">
               <font>
                  <Font size="30.0" />
               </font></Label>
         </children>
      </Pane>
      <HBox layoutX="26.0" layoutY="239.0" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="148.0" prefWidth="749.0" spacing="100.0">
         <children>
            <Pane prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #5f79e6;">
               <children>
                  <ImageView fitHeight="93.0" fitWidth="114.0" layoutX="43.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@images/student.png" />
                     </image>
                  </ImageView>
                  <Button fx:id="studentListBtn" layoutX="45.0" layoutY="113.0" mnemonicParsing="false" onMouseClicked="#handleButtonClick" prefHeight="28.0" prefWidth="93.0" style="-fx-background-color: #252754;" text="Student List" textFill="WHITE">
                     <font>
                        <Font size="14.0" />
                     </font>
                     <padding>
                        <Insets bottom="3.0" top="3.0" />
                     </padding>
                  </Button>
               </children>
            </Pane>
            <Pane layoutX="10.0" layoutY="10.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #ffd56a;">
               <children>
                  <ImageView fitHeight="93.0" fitWidth="114.0" layoutX="43.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@images/student2.png" />
                     </image>
                  </ImageView>
                  <Button layoutX="32.0" layoutY="113.0" mnemonicParsing="false" prefHeight="28.0" prefWidth="115.0" style="-fx-background-color: #806b37;" text="Manage Students" textFill="WHITE">
                     <font>
                        <Font size="14.0" />
                     </font>
                     <padding>
                        <Insets bottom="3.0" top="3.0" />
                     </padding>
                  </Button>
               </children>
            </Pane>
            <Pane layoutX="310.0" layoutY="10.0" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: #79e3aa;">
               <children>
                  <Button layoutX="29.0" layoutY="113.0" mnemonicParsing="false" prefHeight="28.0" prefWidth="121.0" style="-fx-background-color: #40785a;" text="Manage Classroom" textFill="WHITE">
                     <font>
                        <Font size="14.0" />
                     </font>
                     <padding>
                        <Insets bottom="3.0" top="3.0" />
                     </padding>
                  </Button>
                  <ImageView fitHeight="93.0" fitWidth="114.0" layoutX="43.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@images/classroom.png" />
                     </image>
                  </ImageView>
               </children>
            </Pane>
         </children>
      </HBox>
      <MenuBar prefHeight="28.0" prefWidth="800.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Exit">
               <items>
                  <MenuItem mnemonicParsing="false" onAction="#Close_Clicked" text="Exit" />
               </items>
          </Menu>
        </menus>
      </MenuBar>
   </children>
</AnchorPane>


The Error:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3564)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8200(Scene.java:3492)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3860)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
  • In your code, the argument is an ActionEvent, with the name mouseEvent. If you want an action event, you can set the onAction. You might be right about needing a mouse event. Are you loading the same FXML without any error at all? Can you make a compilable and runnable example? – matt Dec 02 '21 at 19:55
  • The file itself is loading without any problems. The error occurs when I try to press the button in order to open a new scene. Also how do I make a compilable and runnable example? – GluhaiaMuha Dec 02 '21 at 20:03
  • Did you try changing the ActionEvent to an actual MouseEvent? Right now, it is an ActionEvent with the name mouseEvent. – matt Dec 02 '21 at 20:08
  • Here is the SO guide to making an example. https://stackoverflow.com/help/minimal-reproducible-example essentially, you need to provide enough, a .java file and .fxml in this case, such that I can run your program and reproduce the error. – matt Dec 02 '21 at 20:10
  • Changed to MouseEvent now I get other error: `Caused by: java.lang.NullPointerException: Location is required. at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106) at SEP1Project.RuntimeTerror/GUI.DashboardController.LoadStage(DashboardController.java:81) at SEP1Project.RuntimeTerror/GUI.DashboardController.handleButtonClick(DashboardController.java:66) ... 42 more` P.S Sorry for the format – GluhaiaMuha Dec 02 '21 at 20:11
  • 1
    Actually, it is best to change onMouseClicked="#handleButtonClick" to onAction="#handleButtonClick". You should be handling actions for buttons, not clicks. An action can be invoked by key presses, but mouse clicks can't. And buttons should be actionable by keys, not just mouse clicks. (Also best to rename the handler from handleButtonClick to handleButtonAction, but that is optional). – jewelsea Dec 02 '21 at 20:13
  • 2
    The load error is unrelated to the type mismatch. The load error is a result of a [wrong path](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other). – jewelsea Dec 02 '21 at 20:14
  • Please google search java naming conventions and apply them thoroughly to all code posted online. – jewelsea Dec 02 '21 at 20:15
  • Don't use System.exit to exit an app, use Platform.exit (read the [Application javadoc](https://openjfx.io/javadoc/17/javafx.graphics/javafx/application/Application.html)). – jewelsea Dec 02 '21 at 20:16
  • I will try to implement everything said above and also make a compilable and runnable example. I'll come back when I finish – GluhaiaMuha Dec 02 '21 at 20:19
  • 1
    I don't recommend switching on the source to define the source of an event, instead provide separate event handling methods for each event, e.g. `@FXML private void onListStudents(ActionEvent e) { loadStage("studentList.fxml"); }`. Some of the comments I have made are not related to actually errors but more to style and convention. For the lookup also see: [where to put resources](https://edencoding.com/where-to-put-resource-files-in-javafx/). – jewelsea Dec 02 '21 at 20:20
  • 1
    Omg by doing this: `@FXML private void onListStudents(ActionEvent e) { loadStage("studentList.fxml"); }` instead of `myHandleButtonAction` method I've made it work. Thank you so much. – GluhaiaMuha Dec 02 '21 at 20:33

0 Answers0