Trying to add actions to my main menu with Scene Builder and Netbeans and I don't understand why I'm getting a InvocationTargetException

百般思念 提交于 2020-11-29 23:58:49

问题


I was following along with a video from school and I swore I added everything they told me to add but I keep getting problems. The only thing that I can think of is that the person in the video is running Netbeans 8.2 while I am running Netbeans 11 and I know that JavaFX isn't as easy on the newer versions of Netbeans...

My project runs until I add actions to my Main Menu buttons and I don't understand why it stops running. This is what my error output says:

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:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    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 java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javafx.fxml.LoadException: Error resolving onAction='#onActionCreateAnimal', either the event handler is not in the Namespace or there is an error in the script.
/C:/Users/ajw51/Documents/NetBeansProjects/example123/build/classes/view/MainMenu.fxml:16

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
    at javafx.fxml/javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:105)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:618)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:778)
    at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at example123.Example123.start(Example123.java:31)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    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)
    ... 1 more
Exception running application example123.Example123
C:\Users\ajw51\Documents\NetBeansProjects\example123\nbproject\build-impl.xml:1330: The following error occurred while executing this line:
C:\Users\ajw51\Documents\NetBeansProjects\example123\nbproject\build-impl.xml:936: Java returned: 1
BUILD FAILED (total time: 2 seconds)

this is my FXML file:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MainMenuController">
   <children>
      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="320.0" spacing="10.0">
         <children>
            <Button mnemonicParsing="false" onAction="#onActionCreateAnimal" prefHeight="50.0" prefWidth="100.0" text="CREATE">
               <font>
                  <Font name="System Bold" size="18.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#onActionDisplayAnimals" prefHeight="50.0" prefWidth="100.0" text="DISPLAY">
               <font>
                  <Font name="System Bold" size="18.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#onActonExit" prefHeight="50.0" prefWidth="100.0" text="EXIT">
               <font>
                  <Font name="System Bold" size="18.0" />
               </font>
            </Button>
         </children>
      </VBox>
   </children>
</AnchorPane>

this is my controller:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

/**
 * FXML Controller class
 *
 * @author ajw51
 */
public class MainMenuController implements Initializable {

    @FXML
    void onActionCreateAnimal(ActionEvent event) {

    }

    @FXML
    void onActionDisplayAnimals(ActionEvent event) {

    }

    @FXML
    void onActonExit(ActionEvent event) {

    }
    
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
    
}

and this is my main:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package example123;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author ajw51
 */
public class Example123 extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/view/MainMenu.fxml"));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.setTitle("Main Menu");
        stage.show();
    }
    
}

I appreciate any help in advance!


回答1:


I found out what my problem was! I imported the wrong thing. It should have been:

import javafx.event.ActionEvent;

instead of:

import java.awt.event.ActionEvent;


来源:https://stackoverflow.com/questions/64398072/trying-to-add-actions-to-my-main-menu-with-scene-builder-and-netbeans-and-i-don

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!