One year ago I’ve wrote JavaFX 2.0 application for my graduation work. But after one year pass I have a problem with code, that worked before.
So, in code I want to get folder and list it’s files:
File dir = new File(getClass().getResource("media/images/backgrounds/").getPath());
File[] files = dir.listFiles();
for (File file : files) {
list.add(file.toURI().toString());
}
and I’m getting java.lang.NullPointerException on line “for (File file : files) {“
System.out.print(getClass().getResource("media/images/backgrounds/").getPath());
returns
file:/D:/JavafxApp1/JavafxApp/dist/run2054723721/JavafxApp.jar!/javafxapp/media/images/backgrounds/
I don’t want make another method for reading files from stream from JAR file.This method of getting files have worked 100% one year ago, but why it doesn’t work now? Thanks!
It won’t work because your folder is in a jar. I suppose one year ago they were not. I think you don’t need to have seperate code for the case where the directory is outside of a jar:
I found this example that shows how you can list the files of a directory using getResourceAsStream. Then, in turn, you could access these resources by getResourceAsStream. You will never know whether you were inside or outside a jar 🙂