当前位置: 首页 > 知识库问答 >
问题:

应用程序启动方法错误中的异常。其他解决方案不起作用

钦永贞
2023-03-14

一切都很顺利,然后我运行我的代码,我得到了这个错误。

我已经尝试过以下解决方案(没有任何效果):

http://stackoverflow.com/questions/22000423/javafx-and-maven-nullpointerexception-location-is-required

应用程序启动方法javafx gui中出现异常

应用程序启动方法中出现异常

JavaFX“location”是必需的。即使它在同一个包中

main.java

package stockBot;

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("resources/main.fxml"));
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
<?xml version="1.0" encoding="UTF-8"?>

<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="545.0" prefWidth="1024.0" styleClass="background" stylesheets="@application.css" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="stockBot.Main">
   <children>
      <HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="50.0" prefWidth="1024.0" styleClass="background-bar" stylesheets="@application.css" />
      <Button maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="20.0" prefWidth="20.0" styleClass="background-button-exit" stylesheets="@application.css" translateX="1000.0" translateY="12.0">
         <graphic>
            <FontAwesomeIconView glyphName="CLOSE" size="16" />
         </graphic>
      </Button>
      <Button maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="20.0" prefWidth="20.0" styleClass="background-button-minus" stylesheets="@application.css" translateX="975.0" translateY="12.0">
         <graphic>
            <FontAwesomeIconView glyphName="MINUS" size="16" translateY="2.0" />
         </graphic>
      </Button>
      <Label text="Stock" textFill="WHITE">
         <font>
            <Font name="Avenir Heavy Oblique" size="43.0" />
         </font>
      </Label>
      <Label text="Bot" textFill="#3fae2a" translateX="110.0">
         <font>
            <Font name="Avenir Heavy Oblique" size="43.0" />
         </font>
      </Label>
      <Button alignment="CENTER" mnemonicParsing="false" styleClass="create-task-btn" stylesheets="@application.css" text="Create Task" textAlignment="CENTER" textFill="#ababab" translateX="450.0" translateY="10.0">
         <font>
            <Font name="Avenir Heavy" size="18.0" />
         </font>
      </Button>
      <TableView prefHeight="440.0" prefWidth="950.0" styleClass="table-main" stylesheets="@application.css" translateX="37.0" translateY="70.0">
        <columns>
          <TableColumn prefWidth="395.4969062805176" styleClass="table-item" text="Product Name" />
          <TableColumn prefWidth="97.50311279296875" styleClass="table-item" text="Current Price" />
            <TableColumn prefWidth="92.45794677734375" styleClass="table-item" text="Lowest Price" />
            <TableColumn prefWidth="99.64276123046875" styleClass="table-item" text="Refresh Delay" />
            <TableColumn prefWidth="137.58929443359375" styleClass="table-item" text="Account Email" />
            <TableColumn prefWidth="123.0" styleClass="table-item" text="Tools" />
        </columns>
      </TableView>
   </children>
</AnchorPane>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" name="lib" level="project" />
  </component>
</module>

错误信息

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:567)
    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:567)
    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:835)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3230)
    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 stockBot.Main.start(Main.java:14)
    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(AccessController.java:389)
    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)
Exception running application stockBot.Main

共有1个答案

汝彭薄
2023-03-14

您不应在对getResources()的调用中包含“resources”。尝试用getClass().getResource(“/main.fxml”)替换它,它应该可以工作(假设没有其他错误)。注意,前面的斜杠很重要。

您的代码当前如何,FXMLLoader正在src/main/resources/resources/main.fxml处查找一个文件,而该文件可能不在您的文件所在的位置。如果您确实有两个名为resources的文件夹,那么您应该使用getClass().getResource(“/resources/main.fxml”)(同样带前导斜杠)。

 类似资料:
  • 我试图创建一个应用程序,但我一直遇到相同的运行时异常。我已经和它斗争了好几天,我不知道如何修复它。任何建议都将不胜感激!这是我在Java中的第一个相当长的项目,所以我决心解决它;只是有相当多的麻烦,我不知道如何克服这一点。

  • 我正在尝试使用intelliJ IDEA创建我的第一个JavaFX程序 我得到这个错误: 这是我的FXML文件: main.java: 这是项目结构: 项目结构 我甚至更改了fxml文件的位置,但他不断得到相同的错误。 FXML文件和Main.java在同一个包中 我是Ubuntu

  • 问题内容: 我只是从JavaFX开始,我正在尝试构建一个带有标签,文本字段和按钮的简单应用程序,单击该按钮即可将标签的值设置为文本字段的值。一切顺利,直到我将控制器连接到Main文件。这是我的代码: Main.java MainWindowView.fxml MainWindowController.java 我已经尝试过在StackOverflow上找到的多个答案,但是我发现的所有答案都是两年前

  • 我创建了一个JavaFX项目,运行时出现错误,我怀疑错误在以下主类中: 以下是错误: 经过一些研究,我发现错误最有可能存在于对FXML加载器的调用中,但是在尝试了几个不同的修复后,我一直无法找到解决方案。创建FXML对象是否错误?谢啦

  • 我的主 LoginController.java 请注意,我还没有在userController.java中编写任何代码,我只是为user.fxml编写了ui 在javafx.fxml.fxmlloader.constructloadexception(fxmlloader.java:2597)在javafx.fxml.fxmlloader.access$100(fxmlloader.java:1

  • FXML 错误 应用程序启动方法java.lang.Reflect.InvocationTargetException位于Sun.Reflect.NativeMethodAccessorImpl.Invoke0(本机方法)位于Sun.Reflect.NativeMethodAccessorImpl.Invoke(NativeMethodAccessorImpl.Invoke)(nativeMeth