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

huangapple go评论84阅读模式
英文:

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

问题

我在学校的视频教程中跟着做,我发誓我添加了他们告诉我的所有内容,但我一直遇到问题。我唯一能想到的是视频中的人正在使用 Netbeans 8.2,而我正在使用 Netbeans 11,我知道在较新版本的 Netbeans 上 JavaFX 不是那么容易...

我的项目在我给主菜单按钮添加动作后运行,我不明白为什么它停止运行。这是我的错误输出:

Application start 方法中的异常
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
Caused by: java.lang.RuntimeException: Application start 方法中的异常
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
...
Caused by: javafx.fxml.LoadException: 无法解析 onAction='#onActionCreateAnimal',可能是事件处理程序不在命名空间中,或者脚本中存在错误。
/C:/Users/ajw51/Documents/NetBeansProjects/example123/build/classes/view/MainMenu.fxml:16
...
Exception running application example123.Example123
C:\Users\ajw51\Documents\NetBeansProjects\example123\nbproject\build-impl.xml:1330: 执行此行时出现以下错误:
C:\Users\ajw51\Documents\NetBeansProjects\example123\nbproject\build-impl.xml:936: Java 返回了: 1
BUILD FAILED (总时间: 2 秒)


这是我的 FXML 文件:

```xml
<?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>

这是我的控制器:

package controller;

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

public class MainMenuController implements Initializable {

    @FXML
    void onActionCreateAnimal(ActionEvent event) {

    }

    @FXML
    void onActionDisplayAnimals(ActionEvent event) {

    }

    @FXML
    void onActonExit(ActionEvent event) {

    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
}

这是我的主类:

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;

public class Example123 extends Application {

    public static void main(String[] args) {
        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();
    }
}

<details>
<summary>英文:</summary>

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&#39;t as easy on the newer versions of Netbeans...

My project runs until I add actions to my Main Menu buttons and I don&#39;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!

</details>


# 答案1
**得分**: 2

我找到问题所在了!我导入错了东西。应该是:

```java
import javafx.event.ActionEvent;

而不是:

import java.awt.event.ActionEvent;
英文:

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;

huangapple
  • 本文由 发表于 2020年10月17日 08:53:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64398072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定