应用程序启动方法JavaFX 1.8中的异常

如何解决应用程序启动方法JavaFX 1.8中的异常

我无法运行在Java版本1.8上运行的JavaFX程序。我注意到,即使我可以在Intellij IDE上运行该程序,也会抛出

java.lang.reflect.InvocationTargetException
尝试在命令行中运行

错误。当我使用“ javac Main.java”时没有错误,但是在我编译它然后键入“ java Main.java”后会抛出错误

这是文件

src文件夹

  • 控制文件夹

    • Controller.java
  • 模型文件夹

    • Database.java
  • 查看文件夹

    • loginview.fxml

    • registerview.fxml

    • mainmenu.fxml

  • Main.java

Main.java

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

import model.Database;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("view/mainmenu.fxml"));
        primaryStage.setTitle("Test Program");
        primaryStage.setScene(new Scene(root,600,600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        Database db = new Database();
        db.testmethod();

        launch(args);
    }
}

Controller.java

package controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Controller {

    public void exitProgram() {
        System.exit(0);
    }

    public void openRegister(ActionEvent event) {
        Parent root;
        try {
            root = FXMLLoader.load(getClass().getClassLoader().getResource("view/registerview.fxml"));
            javafx.stage.Stage stage = new Stage();
            stage.setTitle("Register User");
            stage.setScene(new Scene(root,600));
            stage.setResizable(false);
            stage.show();

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

    public void openLogin(ActionEvent event) {
        Parent root;
        try {
            root = FXMLLoader.load(getClass().getClassLoader().getResource("view/loginview.fxml"));
            javafx.stage.Stage stage = new Stage();
            stage.setTitle("Login User");
            stage.setScene(new Scene(root,600));
            stage.setResizable(false);
            stage.show();

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

    public void close(ActionEvent event) {
        ((Node) (event.getSource())).getScene().getWindow().hide();
    }
}

Database.java

package model;

public class Database {

    public void testmethod(){
        System.out.println("TEST METHOD");
    }

}

loginview.fxml

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
   <children>
      <Label layoutX="134.0" layoutY="145.0" prefHeight="165.0" prefWidth="332.0" text="LOGIN" />
   </children>
</Pane>

mainmenu.fxml

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
   <children>
      <Button fx:id="buttonReg" layoutX="32.0" layoutY="60.0" mnemonicParsing="false" onAction="#openRegister" prefHeight="104.0" prefWidth="521.0" text="OPEN REGISTER" />
      <Button fx:id="buttonLogin" layoutX="32.0" layoutY="211.0" mnemonicParsing="false" onAction="#openLogin" prefHeight="104.0" prefWidth="521.0" text="OPEN LOGIN" />
      <Button fx:id="buttonExit" layoutX="32.0" layoutY="356.0" mnemonicParsing="false" onAction="#exitProgram" prefHeight="104.0" prefWidth="521.0" text="EXIT" />
   </children>
</Pane>

registerview.fxml

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

   <?import javafx.scene.control.Label?>
   <?import javafx.scene.layout.Pane?>


   <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
      <children>
         <Label layoutX="130.0" layoutY="145.0" text="REGISTER" />
      </children>
   </Pane>

命令行错误

TEST METHOD
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:498)
        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:498)
        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$1(LauncherImpl.java:182)
        at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
/C:/Users/Bryan/Desktop/TESTRUN/src/view/mainmenu.fxml:6

        at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
        at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
        at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
        at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
        at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
        at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
        at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
        at Main.start(Main.java:13)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
        ... 1 more
Caused by: java.lang.ClassNotFoundException: controller.Controller
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
        at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
        ... 22 more
Exception running application Main

我注意到数据库在引发错误之前运行。我不知道此错误的原因

解决方法

在Main.java上,

添加

import controller.Controller
,

与其像“ Buttons”这样的按钮,不如尝试将它们放在类中 “ JFXButton”

你有这个:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
    <Button fx:id="buttonReg" layoutX="32.0" layoutY="60.0" mnemonicParsing="false" onAction="#openRegister" prefHeight="104.0" prefWidth="521.0" text="OPEN REGISTER" />
    <Button fx:id="buttonLogin" layoutX="32.0" layoutY="211.0" mnemonicParsing="false" onAction="#openLogin" prefHeight="104.0" prefWidth="521.0" text="OPEN LOGIN" />
    <Button fx:id="buttonExit" layoutX="32.0" layoutY="356.0" mnemonicParsing="false" onAction="#exitProgram" prefHeight="104.0" prefWidth="521.0" text="EXIT" />
</Pane>

尝试一下:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
    <JFXButton fx:id="buttonReg" layoutX="32.0" layoutY="60.0" mnemonicParsing="false" onAction="#openRegister" prefHeight="104.0" prefWidth="521.0" text="OPEN REGISTER" />
    <JFXButton fx:id="buttonLogin" layoutX="32.0" layoutY="211.0" mnemonicParsing="false" onAction="#openLogin" prefHeight="104.0" prefWidth="521.0" text="OPEN LOGIN" />
    <JFXButton fx:id="buttonExit" layoutX="32.0" layoutY="356.0" mnemonicParsing="false" onAction="#exitProgram" prefHeight="104.0" prefWidth="521.0" text="EXIT" />
</Pane>
,

发生auto是因为对类InvocationTargetException的方法getResource()中的方法openRegister()的调用返回了空值。您可以通过简单地打印出方法Controller返回的值来进行检查,例如

getResource()

由于方法System.out.println(getClass().getClassLoader().getResource("view/registerview.fxml")); 构建文件getResource()的绝对路径,然后检查是否存在这样的文件,因此返回null。方法registerview.fxml构建的绝对路径是不是文件的实际路径。因此,您需要更改方法参数,即“ view / registerview.fxml”

我猜下面的解决方案不是唯一的解决方案,但它对我有用。

为了防止得到getResources(),我执行了以下操作。

  1. 我将类InvocationTargetException放在了一个名为Main的程序包中。
    (根据您的问题,我还不清楚jfxtests是否在包装中。)
  2. 我制作了Main的{​​{1}}文件夹和controller文件夹子包。
  3. 我调用方法view,而不是jfxtests
  4. 在类getClass().getResource()中,我更改了传递给方法getClass().getClassLoader().getResource()的参数。

我所做的唯一更改是对Controller类进行了更改。这是我的更改:

getResource()

请注意,由于我更改了类Controller的软件包,因此我还必须更改FXML文件。

package jfxtests.controller;  // changed package

import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Controller {

    public void exitProgram() {
        System.exit(0);
    }

    public void openRegister(ActionEvent event) {
        Parent root = null;
        try {
            root = FXMLLoader.load(getClass().getResource("/jfxtests/view/registerview.fxml"));  // changed this line
            javafx.stage.Stage stage = new Stage();
            stage.setTitle("Register User");
            stage.setScene(new Scene(root,600,600));
            stage.setResizable(false);
            stage.show();

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

    public void openLogin(ActionEvent event) {
        Parent root;
        try {
            root = FXMLLoader.load(getClass().getResource("/jfxtests/view/loginview.fxml"));  // changed this line
            javafx.stage.Stage stage = new Stage();
            stage.setTitle("Login User");
            stage.setScene(new Scene(root,600));
            stage.setResizable(false);
            stage.show();

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

    public void close(ActionEvent event) {
        ((Node) (event.getSource())).getScene().getWindow().hide();
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res