2

I've spent almost 2 days in trying to load files from inside of my netbeans project, but it always gives NullPointException.

currently my directory looks like:
JavaFXApplication:

  • src
        --Manifest (contains Manifest.java)
            --images (inside Manifest package aka Manifest.images)
              --server.jpg (inside images package)

I'm trying to load the server.jpg from images package, but it always return NULL.
Here is the snippet of my code:

try {

        rect.setFill(new ImagePattern(new Image(Manifest.class.getResourceAsStream("images\\server.jpg"))));

    } catch (NullPointerException e) {

    System.out.println(Manifest.class.getResourceAsStream("server.jpg"));

    }

Exactly 2 days before, I saw this code from a YouTube Tutorial, and it doesn't worked. Try many of those solutions from here, but nothing yield for me.And suddenly it worked. Next day, tried to run the same code, and again same NULL error.

Can you guys please help me. I'm totally new to JavaFX. Don't have much experience with it.

androizer
  • 77
  • 9
  • 3
    Aside from anything else, you almost certainly want a forward slash instead of a backslash – Jon Skeet Jan 21 '16 at 14:51
  • See https://docs.oracle.com/javase/8/docs/technotes/guides/lang/resources.html for how to construct resource names. – James_D Jan 21 '16 at 14:54
  • @Jon Skeet AFAIK, "\\" are used for windows based platform, thats why I've used double back slash. – androizer Jan 21 '16 at 15:30
  • 1
    @AkkiMahajan: But you're not asking for a file - you're asking for a resource. Depending on the context, that *might* work - but `/` is much more portable, and will work if the resource is in a jar file, for example. – Jon Skeet Jan 21 '16 at 15:31
  • @JonSkeet Aren't Resources are data files? like audio, image, txt Can u see the ss where I use two of those getResourceAsStream() method? Don't u think, both r different, but they still yields the same result when they r not intended to. dl.dropboxusercontent.com/u/9747708/Images/Server_Client.jpg – androizer Jan 21 '16 at 15:36
  • 1
    @AkkiMahajan: You shouldn't think of them as files on a physical file system. They're data that can be loaded as a stream, but which may well be available in some other way - fetched via a URL, loaded from inside a jar file, etc. You should *not* use backslashes when fetching them. – Jon Skeet Jan 21 '16 at 15:37
  • @JonSkeet I'll use them from now onwards. Thanks. Edit: Have u seen the link in my previous comment? – androizer Jan 21 '16 at 15:41

1 Answers1

2

Use getClass().getClassLoader().getResourceAsStream(""). When you do Manifest.class.getResourceAsStream("images\\server.jpg"), it will try to load the file relative to where the Manifest.class is present.

Ashraff Ali Wahab
  • 1,088
  • 9
  • 19
  • I've tried many of them, and it didn't worked. While after posting this question, I try to run it and it unexpectedly worked out. I've used 2 getResourceAsStream in the sceenshot, both different and it still worked out. https://dl.dropboxusercontent.com/u/9747708/Images/Server_Client.jpg – androizer Jan 21 '16 at 15:27
  • Yes it would work since you have added Manifest to the location `Manifest.class.getResourceAsStream("Manifest\\images\\server.jpg")` which is not same folder you posted in question above. – Ashraff Ali Wahab Jan 21 '16 at 16:47
  • Moreover, when I try to clean the project, and then RUN it, it still doesn't load the images. After verifying the build->classes->Manifest, there was no images folder. Only Manifest.class. So I have to manually copy the images folder from src to build. Why images folder was not into Manifest(class)? – androizer Jan 21 '16 at 17:02
  • Indeed it worked w/o any reason, but both the getResourceAsStream() codes were different if u see them properly. First was not no having getClassLoader() and other was having and relative path was same for both and still they worked.!! How?? JavaFX buggy? – androizer Jan 21 '16 at 17:06
  • Can you try `getClass().getClassLoader().getResourceAsStream("")` without copying the images folder to Manifect folder. But make sure it is under classes folder. – Ashraff Ali Wahab Jan 21 '16 at 17:29
  • getClass().getClassLoader().getResourceAsStream("resources/server.jpg") I've write this and resources folder was in the classes folder. classes->Manifest->Manifest.class classes->resources->server.jpg This worked. But I haven't tried Clean option yet for the project. I'm afraid it will screw up the code again. :p – androizer Jan 21 '16 at 17:42
  • But It worked.! You have any idea? And why resources(images) folder didnt copied to the build folder when compiling/RUN.?? – androizer Jan 21 '16 at 17:46