JavaFX应用程序启动方法中的异常
我正在使用Eclipse编写一些JavaFX代码,当我尝试运行此代码时:JavaFX应用程序启动方法中的异常,javafx,Javafx,我正在使用Eclipse编写一些JavaFX代码,当我尝试运行此代码时: ImageView imgView = new ImageView(new Image(new File(getClass() .getResource("Things\\back.jpg").toExternalForm()).toURI().toString())); eclipse给了我这个错误: Exception in Application start method java.lang.reflect
ImageView imgView = new ImageView(new Image(new File(getClass()
.getResource("Things\\back.jpg").toExternalForm()).toURI().toString()));
eclipse给了我这个错误:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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$155(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at application.OnOffDikra.<init>(OnOffDikra.java:55)
at application.ToggleSwitch.<init>(ToggleSwitch.java:20)
at application.Main.CreateContent(Main.java:19)
at application.Main.start(Main.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
... 1 more
Exception running application application.Main
应用程序启动方法中出现异常
java.lang.reflect.InvocationTargetException
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源)
在sun.reflect.DelegatingMethodAccessorImpl.invoke处(未知源)
位于java.lang.reflect.Method.invoke(未知源)
位于com.sun.javafx.application.LaunchImpl.launchApplicationWithArgs(LaunchImpl.java:389)
位于com.sun.javafx.application.LaunchImpl.launchApplication(LaunchImpl.java:328)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源)
在sun.reflect.DelegatingMethodAccessorImpl.invoke处(未知源)
位于java.lang.reflect.Method.invoke(未知源)
位于sun.launcher.launchelper$FXHelper.main(未知源)
原因:java.lang.RuntimeException:应用程序启动方法中的异常
位于com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:917)
在com.sun.javafx.application.launchempl.lambda$launchApplication$155(launchempl.java:182)
位于java.lang.Thread.run(未知源)
原因:java.lang.NullPointerException
在application.OnOffDikra.(OnOffDikra.java:55)
在application.ToggleSwitch.(ToggleSwitch.java:20)
位于application.Main.CreateContent(Main.java:19)
在application.Main.start(Main.java:27)
在com.sun.javafx.application.launchempl.lambda$launchApplication1$162(launchempl.java:863)
位于com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
位于com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
位于java.security.AccessController.doPrivileged(本机方法)
位于com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
位于com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95)
在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法)
位于com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 还有一个
运行应用程序application.Main时出现异常
操作系统为“Windows 10”
这是我的项目文件的快照
有人能帮我吗?您不应该使用
文件在类路径上加载资源。您可以直接使用getResource()
加载资源
ImageView imgView = new ImageView(
new Image(getClass().getResource("/Things/back.jpg").toExternalForm()));
您不应该使用文件
在类路径上加载资源。您可以直接使用getResource()
加载资源
ImageView imgView = new ImageView(
new Image(getClass().getResource("/Things/back.jpg").toExternalForm()));
因为您试图将图像作为外部文件加载,但图像位于源文件夹中
URL支持的所有URL都可以传递给构造函数。如果
传递的字符串不是有效的URL,而是路径,图像是
在这种情况下,在类路径上搜索
因此,您只需使用一行代码即可加载它:
// load an image in background, displaying a placeholder while it's loading
// (assuming there's an ImageView node somewhere displaying this image)
// The image is located in default package of the classpath
ImageView imageView = new ImageView(new Image("/Things/back.jpg"));
因为您试图将图像作为外部文件加载,但图像位于源文件夹中
URL支持的所有URL都可以传递给构造函数。如果
传递的字符串不是有效的URL,而是路径,图像是
在这种情况下,在类路径上搜索
因此,您只需使用一行代码即可加载它:
// load an image in background, displaying a placeholder while it's loading
// (assuming there's an ImageView node somewhere displaying this image)
// The image is located in default package of the classpath
ImageView imageView = new ImageView(new Image("/Things/back.jpg"));
这对我来说是可行的,当我需要加载映像时,我使用GetResource写入路径witout。您是否将生成的JAR文件移动到其他位置并执行了它?为什么要在当前类加载器中查找newimage()?对我来说是新的事实上。。。我不知道。引用:`如果传递的字符串不是有效的URL,而是路径,那么在这种情况下,将在类路径上搜索图像。`也许你也想在你的答案中添加这个引用。好的,我会的。谢谢你让我们一起来。这对我来说很有效,当我需要加载图像时,我使用GetResource写入路径witout。你是否将生成的JAR文件移到其他位置并执行了它?为什么要在当前类加载器中查找newimage()?对我来说是新的事实上。。。我不知道。引用:`如果传递的字符串不是有效的URL,而是路径,那么在这种情况下,将在类路径上搜索图像。`也许你也想在你的答案中添加这个参考。好的,我会的。谢谢你让我们。