应用程序启动方法java.lang.reflect.InvocationTargetException中的异常

罗伯特·瓦伦西亚

我只是从JavaFX开始,我正在尝试构建一个带有标签,文本字段和按钮的简单应用程序,单击该按钮即可将标签的值设置为文本字段的值。一切顺利,直到我将控制器连接到Main文件。这是我的代码:

Main.java

package application;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;


public class Main extends Application {

    private Stage primaryStage;

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage; // connect primary stage
        mainWindow();
    }

    // main window
    public void mainWindow() {
        try {
            // view
            FXMLLoader loader = new FXMLLoader(Main.class.getResource("/MainWindowView.fxml"));
            AnchorPane pane = loader.load();

            // controller
            MainWindowController mainWindowController = loader.getController();
            mainWindowController.setMain(this);

            // scene on stage
            Scene scene = new Scene(pane);
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

MainWindowView.fxml

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

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainWindowController">
   <children>
      <Label fx:id="label" alignment="CENTER" layoutX="291.0" layoutY="164.0" text="Label" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
         <font>
            <Font size="20.0" />
         </font>
      </Label>
      <HBox alignment="CENTER" layoutX="201.0" layoutY="208.0" spacing="20.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
         <children>
            <TextField fx:id="field" layoutX="201.0" layoutY="208.0" />
            <Button layoutX="381.0" layoutY="208.0" mnemonicParsing="false" onAction="#handleButton" text="Change Text" />
         </children>
      </HBox>
   </children>
</AnchorPane>

MainWindowController.java

package application;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class MainWindowController {

    // views
    @FXML private Label label;
    @FXML private TextField field;

    private Main main;

    // connect main class to controller
    public void setMain(Main main) {
        this.main = main; 
    }

    // assign text field text to label on button click
    public void handleButton() {
        String text = field.getText();
        label.setText(text);
        field.clear();
    }
}

我已经尝试过在StackOverflow上找到的多个答案,但是我发现的所有答案都是两年前的,并且对我的代码没有任何积极影响。

编辑:堆栈跟踪在这里:

Exception in Application start method
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:497)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at application.Main.mainWindow(Main.java:27)
    at application.Main.start(Main.java:19)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application application.Main
罗伯特·瓦伦西亚

对于将来遇到同样完全相同的问题的人,如James_D和其他答案提供者所述,删除路径开头的“ /”可解决问题,因此请使用

FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));

代替

FXMLLoader loader = new FXMLLoader(Main.class.getResource("/MainWindowView.fxml"));

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

javafx中的java.lang.reflect.InvocationTargetException中的异常

来自分类Dev

应用程序构造函数java.lang.reflect.InvocationTargetException中的JavaFX异常

来自分类Dev

JavaFX:线程“ JavaFX Application Thread”中的异常java.lang.RuntimeException:java.lang.reflect.InvocationTargetException

来自分类Dev

Android中的java.lang.reflect.invocationtargetException

来自分类Dev

错误yarn.ApplicationMaster:用户类引发异常:java.lang.reflect.InvocationTargetException java.lang.reflect.InvocationTargetException

来自分类Dev

错误yarn.ApplicationMaster:用户类引发异常:java.lang.reflect.InvocationTargetException java.lang.reflect.InvocationTargetException

来自分类Dev

如何在Maven中解决java.lang.reflect.InvocationTargetException

来自分类Dev

Roboelectric java.lang.RuntimeException:java.lang.reflect.InvocationTargetException

来自分类Dev

启动Activity时出现java.lang.reflect.InvocationTargetException

来自分类Dev

从switchyard中的WSDL创建Java文件时出现java.lang.reflect.InvocationTargetException

来自分类Dev

如何在 JavaFX 中交换控制器?java.lang.reflect.InvocationTargetException

来自分类Dev

为什么显示“ java.lang.reflect.InvocationTargetException”

来自分类Dev

显示错误原因:java.lang.reflect.InvocationTargetException

来自分类Dev

从TextField获取值时java.lang.reflect.InvocationTargetException-Javafx

来自分类Dev

会议室DB:java.lang.reflect.InvocationTargetException

来自分类Dev

如何修复/解决java.lang.reflect.InvocationTargetException

来自分类Dev

签名的jar抛出java.lang.reflect.InvocationTargetException

来自分类Dev

调用ScalaTest时java.lang.reflect.InvocationTargetException

来自分类Dev

java.lang.reflect.invocationtargetexception在Junit中导致null

来自分类Dev

具有DataTemplate的ListView-java.lang.reflect.InvocationTargetException

来自分类Dev

出现错误 java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

来自分类Dev

java.lang.reflect.Proxy:巨大的异常堆栈跟踪

来自分类Dev

JavaFX,Scene Builder(IntelliJ)程序从StackOverFlow抛出java.lang.reflect.InvocationTargetException

来自分类常见问题

线程“主”中的异常java.lang.UnsatisfiedLinkError:'void org.lwjgl.opengl.WindowsDisplay.setWindowProc(java.lang.reflect.Method)'

来自分类Dev

什么是`4161`在java.lang.reflect.Method中修改的平均

来自分类Dev

投java.lang.reflect.Method中的功能接口

来自分类Dev

使用Kotlin-Mokito库时java.lang.reflect.InvocationTargetException

来自分类Dev

ApachePOI生成org.apache.poi.POIXMLException:java.lang.reflect.InvocationTargetException

来自分类Dev

越来越java.lang.reflect.InvocationTargetException同时加入一个按钮布局

Related 相关文章

  1. 1

    javafx中的java.lang.reflect.InvocationTargetException中的异常

  2. 2

    应用程序构造函数java.lang.reflect.InvocationTargetException中的JavaFX异常

  3. 3

    JavaFX:线程“ JavaFX Application Thread”中的异常java.lang.RuntimeException:java.lang.reflect.InvocationTargetException

  4. 4

    Android中的java.lang.reflect.invocationtargetException

  5. 5

    错误yarn.ApplicationMaster:用户类引发异常:java.lang.reflect.InvocationTargetException java.lang.reflect.InvocationTargetException

  6. 6

    错误yarn.ApplicationMaster:用户类引发异常:java.lang.reflect.InvocationTargetException java.lang.reflect.InvocationTargetException

  7. 7

    如何在Maven中解决java.lang.reflect.InvocationTargetException

  8. 8

    Roboelectric java.lang.RuntimeException:java.lang.reflect.InvocationTargetException

  9. 9

    启动Activity时出现java.lang.reflect.InvocationTargetException

  10. 10

    从switchyard中的WSDL创建Java文件时出现java.lang.reflect.InvocationTargetException

  11. 11

    如何在 JavaFX 中交换控制器?java.lang.reflect.InvocationTargetException

  12. 12

    为什么显示“ java.lang.reflect.InvocationTargetException”

  13. 13

    显示错误原因:java.lang.reflect.InvocationTargetException

  14. 14

    从TextField获取值时java.lang.reflect.InvocationTargetException-Javafx

  15. 15

    会议室DB:java.lang.reflect.InvocationTargetException

  16. 16

    如何修复/解决java.lang.reflect.InvocationTargetException

  17. 17

    签名的jar抛出java.lang.reflect.InvocationTargetException

  18. 18

    调用ScalaTest时java.lang.reflect.InvocationTargetException

  19. 19

    java.lang.reflect.invocationtargetexception在Junit中导致null

  20. 20

    具有DataTemplate的ListView-java.lang.reflect.InvocationTargetException

  21. 21

    出现错误 java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

  22. 22

    java.lang.reflect.Proxy:巨大的异常堆栈跟踪

  23. 23

    JavaFX,Scene Builder(IntelliJ)程序从StackOverFlow抛出java.lang.reflect.InvocationTargetException

  24. 24

    线程“主”中的异常java.lang.UnsatisfiedLinkError:'void org.lwjgl.opengl.WindowsDisplay.setWindowProc(java.lang.reflect.Method)'

  25. 25

    什么是`4161`在java.lang.reflect.Method中修改的平均

  26. 26

    投java.lang.reflect.Method中的功能接口

  27. 27

    使用Kotlin-Mokito库时java.lang.reflect.InvocationTargetException

  28. 28

    ApachePOI生成org.apache.poi.POIXMLException:java.lang.reflect.InvocationTargetException

  29. 29

    越来越java.lang.reflect.InvocationTargetException同时加入一个按钮布局

热门标签

归档