javafx 中 invocationtargetexception
本篇文章介绍如何解决 javafx 中的 invocabletargetexception。
修复 javafx 中的 inspirationtargetexception
当我们使用 javafx 时,会发生 invokingtargetexception
异常。 出现该异常的原因有:
- 当我们在 fxml 文件的路径名开头写入正斜杠时,或者给定的路径不正确或不完整。
- 当我们尝试使用动态根但尚未在 fxml 加载器上设置根时。
- 当我们错过 javafx 模块时。
让我们尝试一个在 javafx 中抛出 inspirationtargetexception
的示例。
package jiyik;
import java.io.file;
import javafx.application.application;
import javafx.fxml.fxmlloader;
import javafx.scene.parent;
import javafx.scene.scene;
import javafx.stage.stage;
public class example extends application {
@override
public void start(stage demostage) throws exception {
system.out.println("start()");
parent demoroot = fxmlloader.load(getclass().getresource("jiyik.fxml"));
demostage.settitle("demo");
scene demoscene = new scene(demoroot, 200, 150);
demostage.setscene(demoscene);
demostage.show();
}
}
jiyik.fxml 文件是:
<anchorpane maxheight="-infinity" maxwidth="-infinity" minheight="-infinity" minwidth="-infinity" prefheight="400.0" prefwidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<children>
<scrollpane layoutx="178.0" layouty="68.0" prefheight="400.0" prefwidth="600.0" anchorpane.bottomanchor="0.0" anchorpane.leftanchor="0.0" anchorpane.rightanchor="0.0" anchorpane.topanchor="0.0">
<content>
<tableview prefheight="350.0" prefwidth="590.0">
<columns>
<tablecolumn prefwidth="130.0" text="first name" />
<tablecolumn prefwidth="130.0" text="last name" />
<tablecolumn prefwidth="330.0" text="id" />
columns>
tableview>
content>
scrollpane>
children>
anchorpane>
上面的代码将抛出 invocabletargetexception,因为它找不到 jiyik.fxml 文件。 查看输出:
start()
exception in application start method
exception in thread "main" java.lang.reflect.invocationtargetexception
at java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke0(native method)
at java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:77)
at java.base/jdk.internal.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)
at java.base/java.lang.reflect.method.invoke(method.java:568)
at java.base/sun.launcher.launcherhelper$fxhelper.main(launcherhelper.java:1071)
caused by: java.lang.runtimeexception: exception in application start method
at javafx.graphics@18.0.1/com.sun.javafx.application.launcherimpl.launchapplication1(launcherimpl.java:901)
at javafx.graphics@18.0.1/com.sun.javafx.application.launcherimpl.lambda$launchapplication$2(launcherimpl.java:196)
at java.base/java.lang.thread.run(thread.java:833)
caused by: java.lang.nullpointerexception: location is required.
at javafx.fxml@18.0.1/javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3324)
at javafx.fxml@18.0.1/javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3287)
at javafx.fxml@18.0.1/javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3255)
at javafx.fxml@18.0.1/javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3227)
at javafx.fxml@18.0.1/javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3203)
at javafx.fxml@18.0.1/javafx.fxml.fxmlloader.load(fxmlloader.java:3196)
at delftstack.example.start(example.java:15)
at javafx.graphics@18.0.1/com.sun.javafx.application.launcherimpl.lambda$launchapplication1$9(launcherimpl.java:847)
at javafx.graphics@18.0.1/com.sun.javafx.application.platformimpl.lambda$runandwait$12(platformimpl.java:484)
at javafx.graphics@18.0.1/com.sun.javafx.application.platformimpl.lambda$runlater$10(platformimpl.java:457)
at java.base/java.security.accesscontroller.doprivileged(accesscontroller.java:399)
at javafx.graphics@18.0.1/com.sun.javafx.application.platformimpl.lambda$runlater$11(platformimpl.java:456)
at javafx.graphics@18.0.1/com.sun.glass.ui.invokelaterdispatcher$future.run(invokelaterdispatcher.java:96)
at javafx.graphics@18.0.1/com.sun.glass.ui.win.winapplication._runloop(native method)
at javafx.graphics@18.0.1/com.sun.glass.ui.win.winapplication.lambda$runloop$3(winapplication.java:184)
... 1 more
现在,如果我们尝试通过给出文件 jiyik.fxml 的绝对路径来解决它,它可能会解决问题,但对于某些系统,它仍然会导致相同的问题。 针对该错误的最佳ag捕鱼王app官网的解决方案如下:
- 将 jiyik.fxml 文件复制到类的同一目录中。 例如,如果类文件位于 jiyik 包中,则应将fxml文件复制到 project/src/jiyik 文件夹中。
- 在 ide 中刷新项目。 例如,在 eclipse 中,右键单击项目名称并选择“刷新”。
- 运行相同的程序,错误就会解决。
按照上述ag捕鱼王app官网的解决方案后的输出是:
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
如何在 java 中延迟几秒钟的时间
发布时间:2023/12/17 浏览次数:217 分类:java
-
本篇文章主要介绍如何在 java 中制造程序延迟。本教程介绍了如何在 java 中制造程序延时,并列举了一些示例代码来了解它。
如何在 java 中把 hashmap 转换为 json 对象
发布时间:2023/12/17 浏览次数:187 分类:java
-
它描述了允许我们将哈希图转换为简单的 json 对象的方法。本文介绍了在 java 中把 hashmap 转换为 json 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 json 对象的详细例子。
如何在 java 中按值排序 map
发布时间:2023/12/17 浏览次数:171 分类:java
-
本文介绍了如何在 java 中按值对 map 进行排序。本教程介绍了如何在 java 中按值对 map
进行排序,并列出了一些示例代码来理解它。
如何在 java 中打印 hashmap
发布时间:2023/12/17 浏览次数:192 分类:java
-
本帖介绍了如何在 java 中打印 hashmap。本教程介绍了如何在 java 中打印 hashmap 元素,还列举了一些示例代码来理解这个主题。
在 java 中更新 hashmap 的值
发布时间:2023/12/17 浏览次数:146 分类:java
-
本文介绍了如何在 java 中更新 hashmap 中的一个值。本文介绍了如何在 java 中使用 hashmap 类中包含的两个方法-put() 和 replace() 更新 hashmap 中的值。
java 中的 hashmap 和 map 之间的区别
发布时间:2023/12/17 浏览次数:79 分类:java
-
本文介绍了 java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 java 中 map 和 hashmap 之间的主要区别。在 java 中,map 是用于以键值对存储数据的接口,
在 java 中获取用户主目录
发布时间:2023/12/17 浏览次数:218 分类:java
-
这篇文章向你展示了如何在 java 中获取用户主目录。本教程介绍了如何在 java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。
java 中 size 和 length 的区别
发布时间:2023/12/17 浏览次数:179 分类:java
-
这篇文章教你如何知道 java 中大小和长度之间的区别。本教程介绍了 java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。
java 中的互斥锁
发布时间:2023/12/17 浏览次数:111 分类:java
-
了解有关 java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,