【问题标题】:Issue displaying a map with java [duplicate]用java显示地图的问题[重复]
【发布时间】: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

标签: java svg webview


【解决方案1】:

我不能从粘贴的代码中肯定地说,但我想我可以帮助你调试它。您已将 webEngine.load(this.getClass().getResource("../../resources/worldMap.html").toExternalForm()); 确定为您的一个 cmets 中的错误行。

NPE 意味着您在该行中使用的其中一个东西以一种意想不到的方式为空。您是否使用带有调试器的 IDE?在该行上设置断点并评估每个子组件。

如果没有,你真的应该尝试一个。我使用 intellij 并发现它使我更加有效和高效。在此期间,hackiest 最简单的解决方法是使用一堆打印语句

System.out.println("webEngine" + webEngine);
System.out.println("class" + this.getClass());
System.out.println("resource" + this.getClass().getResource("../../resources/worldMap.html"));
System.out.println("loaded" + webEngine.load(this.getClass().getResource("../../resources/worldMap.html"));

其中一项将为空,下一行将抛出 NPE。这样,您就会知道堆栈中的哪个位置有问题。找到后请回帖。我猜想要么使用 this.getClass.getResource 是错误的,要么你认为路径字符串中的相对路径开始的位置是错误的(通常它是相对于你的 .class 路径的写入位置)。

【讨论】:

  • 好的,感谢您的建议。所以我做了你告诉我的,这是空的资源。 + 这是来自 .class/.java 的路径,我还尝试了“../resources/worldMap.html”和“resources/worldMap.html”和“worldMap.html”,与 .svg 图像相同,甚至与正如我所说的来自互联网的 URL(我也尝试了绝对路径),所以我怀疑问题是否来自路径..
  • 你的意思是第三个打印语句是“resource null”?你可以制作一个文件对象并打印你的 cwd 吗?我也更熟悉使用 .getClass.getClassLoader.getResourceAsStream 的人
  • 是的,第三个打印语句就是这样。当我创建一个文件时,它与 .project 在同一目录中创建,因此路径应该是“resources/worldMap.html”,但即使使用此路径,资源仍然为空。
  • 我也试过这个:String path = new java.io.File(".").getCanonicalPath(); System.out.println(路径);它打印项目目录的路径..
【解决方案2】:

您是否尝试过使用绝对路径来加载资源?您认为代码执行的位置和实际执行的位置可能不同。

【讨论】:

    【解决方案3】:

    好的,我终于找到了答案。 我的问题其实是Class.getResource() returns null的重复@

    答案是图像必须与我应用“getclass”的类所在的包位于同一目录中,而它位于项目的根目录中。

    感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      相关资源
      最近更新 更多