JavaFXInvocationTargetError

JavaFXInvocationTargetError,java,maven,intellij-idea,javafx,Java,Maven,Intellij Idea,Javafx,起初JavaFX运行良好,但当我将Maven添加到项目中时,我不得不重新配置依赖项,现在它给了我以下堆栈跟踪: java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.i

起初JavaFX运行良好,但当我将Maven添加到项目中时,我不得不重新配置依赖项,现在它给了我以下堆栈跟踪:

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:473)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    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:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3246)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)
    at sample.Main.start(Main.java:13)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    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:175)
    ... 1 more
Exception running application sample.Main


这是我的主要课程:

导入javafx.application.application;
导入javafx.fxml.fxmloader;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.stage.stage;
公共类主扩展应用程序{
@凌驾
public void start(Stage primaryStage)引发异常{
父根=FXMLLoader.load(getClass().getResource(“sample.fxml”);
setTitle(“你好世界”);
primaryStage.setScene(新场景(根、,
400, 400));
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}
sample.fxml:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Label layoutX="102.0" layoutY="26.0" prefHeight="39.0" prefWidth="182.0" text="Dictionary application" AnchorPane.bottomAnchor="335.0" AnchorPane.leftAnchor="102.0" AnchorPane.rightAnchor="116.0" AnchorPane.topAnchor="26.0">
         <font>
            <Font size="18.0" />
         </font>
      </Label>
      <Label layoutX="224.0" layoutY="65.0" prefHeight="18.0" prefWidth="154.0" text="Made by Edita Komarova" />
      <TextField fx:id="definitionsfield" layoutX="35.0" layoutY="106.0" AnchorPane.bottomAnchor="268.4" AnchorPane.leftAnchor="35.0" AnchorPane.rightAnchor="216.2" AnchorPane.topAnchor="106.0" />
      <TextArea fx:id="text" layoutX="93.0" layoutY="146.0" prefHeight="200.0" prefWidth="200.0" />
      <Button layoutX="206.0" layoutY="106.0" mnemonicParsing="false" onAction="#handleDefinitions" text="Get definition" />
      <Button layoutX="284.0" layoutY="361.0" mnemonicParsing="false" onAction="#handleHistory" text="History" />
   </children>
</AnchorPane>


但它不起作用。 在项目中使用maven时,可能会有点不同?
如果您使用maven,我将非常感谢您的帮助,fxml资源应该位于如下路径中:

src/main/resources/fxml/sample.fxml

此外,.fxml文件应该重新引用类控制器


它适合Intellij吗?它不允许我从
更改,也仍然给我相同的错误。还添加了示例fxml文件OK。您可以使用来设计/编辑UI(fxml)。我假设sample.fxml在src/main/resources目录中,否则,将其移动到那里。我不知道类sample.Controller是否存在,但如果不存在,请删除fx:Controller=“sample.Controller”并重试;它应该更适合你的情况。这些也可能是相关的:以及