Error that occurred when I ran the code
Exception in thread "main" 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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:873) Caused by: java.lang.RuntimeException: Unable to construct Application instance: class sample.Main at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.NoSuchMethodException: sample.Main.<init>() at java.lang.Class.getConstructor0(Class.java:3082) at java.lang.Class.getConstructor(Class.java:1825) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$7(LauncherImpl.java:818) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$4(WinApplication.java:185) ... 1 more XML file that contains the buttons and the label etc.
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.Menu?> <?import javafx.scene.control.MenuBar?> <?import javafx.scene.control.MenuItem?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="450.0" prefWidth="615.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1"> <children> <MenuBar layoutY="2.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <menus> <Menu disable="true" mnemonicParsing="false" text="File"> <items> <MenuItem mnemonicParsing="false" text="Close" /> </items> </Menu> <Menu disable="true" mnemonicParsing="false" text="Edit"> <items> <MenuItem mnemonicParsing="false" text="Delete" /> </items> </Menu> <Menu disable="true" mnemonicParsing="false" text="Help"> <items> <MenuItem mnemonicParsing="false" text="About" /> </items> </Menu> </menus> </MenuBar> <Button layoutX="190.0" layoutY="173.0" mnemonicParsing="false" onAction="#headmaterLogin" prefHeight="52.0" prefWidth="195.0" text="Headmaster" /> <Button layoutX="190.0" layoutY="102.0" mnemonicParsing="false" onAction="#adminLogin" prefHeight="52.0" prefWidth="195.0" text="Admin" /> <Button layoutX="190.0" layoutY="305.0" mnemonicParsing="false" onAction="#financedepartmentLogin" prefHeight="52.0" prefWidth="195.0" text="Finance department" /> <Button layoutX="190.0" layoutY="236.0" mnemonicParsing="false" onAction="#headdepartmentLogin" prefHeight="52.0" prefWidth="195.0" text="Head of department" /> <Label layoutX="216.0" layoutY="52.0" prefHeight="35.0" prefWidth="143.0" text="Select an option to Log in" /> </children> </AnchorPane> Main function. That creates the screen. As this function is called when the code is executed.
package sample; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("mainMenu.fxml")); primaryStage.setTitle("Main menu"); primaryStage.setScene(new Scene(root, 300, 275)); primaryStage.show(); } } Controllers, these are the methods that would link to the button events. Resulting in something occurring when the buttons are clicked.
package sample; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; public class mainMenuController { @FXML private ResourceBundle resources; @FXML private URL location; @FXML protected void adminLogin(ActionEvent event) { int count = 0; System.out.print(count); } @FXML protected void financedepartmentLogin(ActionEvent event) { } @FXML protected void headdepartmentLogin(ActionEvent event) { } @FXML protected void headmasterLogin(ActionEvent event) { } @FXML void initialize() { } } https://stackoverflow.com/questions/66020151/every-time-i-try-to-execute-the-code-to-produce-the-application-i-get-this-error February 03, 2021 at 09:49AM
没有评论:
发表评论