在JavaFX的实际应用中,使用URL,遇到了null问题,请问如何解决
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:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:467)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:366)
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:1082)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:903)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "cssurl" is null
at com.example.practicumcode/com.example.practicumcode.Main.start(Main.java:31)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:849)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
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:174)
... 1 more
Exception running application com.example.practicumcode.Main
不知道你这个问题是否已经解决, 如果还没有解决的话:答案:
针对JavaFX应用中使用URL遇到的null问题,可能是由于文件路径不正确引起的。可以尝试使用相对路径或绝对路径来加载文件和资源。另外,如果使用JavaFX 11及以上版本,可以使用第三方库Gluon提供的Gluon Attach库来实现简单的文件操作,该库提供了一系列实用程序和API,用于在JavaFX应用中访问本地和云存储。
示例代码:
使用相对路径:
File file = new File("src/main/resources/example.txt"); URL url = file.toURI().toURL();
使用绝对路径:
File file = new File("C:/example.txt"); URL url = file.toURI().toURL();
使用Gluon Attach:
String fileName = "example.txt"; URL url = AttachServiceFactory.getAttachService().getPrivateStorage().getStorageLocation(fileName);
参考资料:
https://openjfx.io/javadoc/11/javafx.graphics/javafx/scene/image/Image.html https://gluonhq.com/products/attach/