-1

In this fourm are a lot of questions but none is similar to mine.

I always get a NullPointerException if i try to jump two folders up in the directory. If i just jump up one folder, the program finds my .html file...

This is the java part where I load my html File. This Java file is in the package src and there in the package application.

@FXML
private void initialize() {
    webEngine = webView.getEngine();
    try {
        webEngine.load(getClass().getResource("../../local/offer/test.html").toExternalForm());
    }catch (Exception e) {
        e.printStackTrace();
    }
}


- src
   -application
       .java
-bin
-local
   -offer
       test.html

This is my folder structure. My .java is in the folder application. If i use ../ i go one folder up, so i search on the level of the application folder. No i use ../ again to go one folder up again, to search on thje src folder level. In my opinion no i can use local/offer/test.html.

So ../../local/offer/test.html should work in my opinion, but it doesn't

enter image description here

Henning
  • 39
  • 8
  • getClass().getResource(path) will fetch resources from the classpath & not file system. Right ? – TechFree Jun 07 '19 at 09:37
  • yes that should be correct. But it works to go up to the previous folder. But going than one folder up again, so to the previous previous folder does not work – Henning Jun 07 '19 at 09:58
  • Possible duplicate of [How to use getClass().getResource() method](https://stackoverflow.com/questions/12103371/how-to-use-getclass-getresource-method) – TehMattGR Jun 07 '19 at 11:05
  • instead of marking my question as duplicate, which it isnt really, just give me an answer please. What do I have to write instead of "../../local/offer/test.html"? – Henning Jun 07 '19 at 11:16
  • Your classpath consists of `/application/YourClass.java`. You can't move up beyond `/`. You need to move your resources onto your classpath instead, or access them as files instead of as classpath resources. – Mark Rotteveel Jun 07 '19 at 14:12
  • Thank you for your answer Mark :) I'm new to java, just learned it a year ago. So what especially do i have to do? – Henning Jun 07 '19 at 15:06

1 Answers1

0

When using "../" means that you search at the previous folder so with "../test.html" you exit from the application folder and search for a file named test.html on the folder you show in the second picture.

With the "../../test.html" you search two folders behind the application

Also the getClass().getResource() searches on the project's folder instead of the classes

TehMattGR
  • 1,710
  • 2
  • 7
  • 19
  • Ok, i tried the first thing in your comment and it worked. But the problem is, with ../../ it doesnt work... With ../../ i should be in the folder as shown in my first picture. But that doesnt work – Henning Jun 07 '19 at 09:56
  • I have also tried "local/test.html" but that also does not work – Henning Jun 07 '19 at 09:57
  • with "../../" you go two folders behind the project's. For example your projects is on folder "foo/buzz/application" so from application folder you got to foo folder but your test.html is located on the buzz so it will return error. – TehMattGR Jun 07 '19 at 10:23
  • yes i understand what you mean, but it doesn't work. I have edited my questions and showed my folder structure to make it more clearly. thank you so far – Henning Jun 07 '19 at 10:42