【问题标题】:JavaFX InvocationTargetError [duplicate]JavaFX InvocationTargetError [重复]
【发布时间】:2021-04-07 23:09:53
【问题描述】:

起初 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


这是我的主要课程:

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root,
                400, 400));
        primaryStage.show();
    }


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


我试过了:

    Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));

但它没有用。 当您在项目中使用 maven 时,可能会有些不同? 非常感谢您的帮助

【问题讨论】:

标签: java maven intellij-idea javafx


【解决方案1】:

当你使用 maven 时,fxml 资源应该在这样的路径中:

src/main/resources/fxml/sample.fxml

除了 .fxml 文件应该重新引用类控制器。

&lt;VBox xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SampleController"&gt;

【讨论】:

  • 它适合 Intellij 吗?它不允许我从 &lt;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"&gt; 更改,也仍然给我同样的错误。
  • 还添加了示例 fxml 文件
  • 好的。您可以使用gluonhq.com/products/scene-builder 来设计/编辑您的 UI (fxml)。我想 sample.fxml 在 src/main/resources 目录中,否则,将它移到那里。我不知道 sample.Controller 类是否存在,但如果不存在则删除 fx:controller="sample.Controller" 并重试。
猜你喜欢
  • 1970-01-01
  • 2014-08-29
  • 2020-12-18
  • 2022-01-05
  • 2016-03-04
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 1970-01-01
相关资源
最近更新 更多