【发布时间】:2017-12-04 12:37:29
【问题描述】:
我目前正在尝试制作一个类似风险的游戏,当我尝试显示地图(矢量图像)时,我有一个 NullPointerException,我完全不明白它是如何解决的。 =/ 这是代码:
public class Test1 extends Stage {
private BorderPane root = new BorderPane();
private WebView browser = new WebView();
public Test1(){
this.setTitle("Test1");
this.setScene(new Scene(content()));
}
Parent content(){
WebEngine webEngine = browser.getEngine();
webEngine.load(this.getClass().getResource("../../resources/worldMap.html").toExternalForm());
root.setCenter(browser);
return root;
}
}
和错误:
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 hmi.Test1.content(Test1.java:23)
at hmi.Test1.<init>(Test1.java:18)
at Main.start(Main.java:13)
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 Main
所以它似乎是由链接“../../resources/worldMap.html”引起的,但它确实导致了文件。我还尝试使用 svg 文件或 url(这个:https://upload.wikimedia.org/wikipedia/commons/8/80/World_map_-_low_resolution.svg) 我仍然有完全相同的错误。 尽管我在互联网上进行了研究,但我一直坚持这一天,所以我希望你能帮助我。 谢谢!
【问题讨论】:
-
这一切似乎都是由 NullPointerException 引起的。你检查过
at hmi.Test1.content(Test1.java:23)发生了什么吗? -
是的,这是这一行:webEngine.load(this.getClass().getResource("../../resources/worldMap.html").toExternalForm());这就是为什么我推断问题来自链接,但我不知道为什么以及如何解决它。
-
哼。调试?我怎么做 ?对我有帮助吗?
-
如果你不知道调试是什么,你应该尽快学会。对于程序员来说,这是一项必要的基本技能。这也不是可以在此处的评论部分轻松教授的东西。我会说开始阅读教程和练习。不过,对于您的问题,您是否确保 webEngine 不为空? getClass 返回的是什么?获取资源呢?和外部形式?我真的怀疑它与该文件有关,除非它找不到该文件并且默认返回 null 。 有些东西返回 null,然后您正在尝试使用该 null。