-1

I currently am trying to make a risk-like game and as I try to display a map (vectorial image) I have a NullPointerException that I do not understand at all how it can be solved. =/ Here's the code :

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;
    }
}

and the error :

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

So it seems to be caused by the link "../../resources/worldMap.html" but it really leads to the file. I also tried with a svg file or with an url (this one : https://upload.wikimedia.org/wikipedia/commons/8/80/World_map_-_low_resolution.svg ) and I still have the exact same error. It's been a day I'm stuck on that despite my researchs on internet, so I hope you will be able to help me. Thanks !

Sianurh
  • 1
  • 1
  • 1
    It all appears to be caused by a NullPointerException. Have you checked what's going on `at hmi.Test1.content(Test1.java:23)`? – Carcigenicate Jun 30 '17 at 14:16
  • Yes this is this line : webEngine.load(this.getClass().getResource("../../resources/worldMap.html").toExternalForm()); That's why I deduced that the issue came from the link, but I don't know why and how to solve it. – Sianurh Jun 30 '17 at 14:21
  • Hum. Debugging ? How do I do that ? Will it help me ? – Sianurh Jun 30 '17 at 14:22
  • If you don't know what debugging is, you should learn that ASAP. It's a necessary, fundamental skill for a programmer. It's also not something that can be easily taught in the comment section here. I'd say start reading tutorials and practicing. For your question here though, have you ensured that webEngine isn't null? What about what getClass returns? What about getResource? And toExternalForm? I really doubt it's related to that file, unless it can't find the file and is defaulting on returning null instead. **Something is returning null, then you're attempting to use that null**. – Carcigenicate Jun 30 '17 at 14:25

3 Answers3

0

I can't say for certain from the pasted code, but I think I can help you debug it. You've identified webEngine.load(this.getClass().getResource("../../resources/worldMap.html").toExternalForm()); as the errant line in one of your comments.

The NPE means one of the things you are using in that line is null in an unexpected way. Are you using an IDE with a debugger? Set a break-point on that line and evaluate each subcomponent.

If not, you really should try one. I use intellij and find it makes me far more effective and efficient. In the interim, the hackiest simplest way to figure this out is with a bunch of print statements

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"));

One of these things will be null and the next line will throw an NPE. That way, you'll know where in the stack you have a problem. Please post back when you find it. I would guess that either using the this.getClass.getResource is wrong or the place you think the relative path starts from in your path string is wrong (usually it is relative to where your .class paths are written).

  • Ok thanks for the advices. So I did what you told me, and this is the resource which is null. + this is the path from the .class/.java, I also tried "../resources/worldMap.html" and "resources/worldMap.html" and "worldMap.html" and same with the .svg image and even with an URL from internet as I said (I also tried an absolute path), so I doubt that the issue come from the path despite all.. – Sianurh Jun 30 '17 at 15:04
  • You mean the third print statement is "resource null"? Can you make a file object and print your cwd? I'm also more familiar with people using .getClass.getClassLoader.getResourceAsStream – David Winton Jun 30 '17 at 15:24
  • Yes the third print statement is that. And when I create a file it is created in the same directory as the .project, so the path should be "resources/worldMap.html" but even with this path the resource is still null. – Sianurh Jun 30 '17 at 15:49
  • I also tried this : String path = new java.io.File(".").getCanonicalPath(); System.out.println(path); And it prints the path to the project directory.. – Sianurh Jun 30 '17 at 16:46
0

Have you tried using an absolute path instead to load your resource? It's possible where you think your code is executing from and where it's actually executing from are different.

Thorntonz
  • 46
  • 2
0

Ok, I finally found out the answer. My question is actually a duplicate of Class.getResource() returns null

And the answer was that the image had to be in the same directory as the package where the class on which I apply the "getclass" is, while it was in the root of the project.

Thank you for your help though !

Sianurh
  • 1
  • 1