I was running this code and it works fine within the NetBeans IDE but as soon as I built the JAR file and ran it by clicking, it crashed and threw a NullPointerException
String sepChar = File.separator;
File homeFolder = new File(System.getProperty("user.dir"));
File dataFolder = new File(homeFolder.getAbsolutePath() + sepChar + "Saved Images");
System.err.println("data files is folder: "+dataFolder.isDirectory());
System.err.println("data files is file: "+dataFolder.isFile());
System.err.println("data files: "+dataFolder.listFiles());
System.err.println("data folder: "+dataFolder);
System.err.println("data folder path : "+dataFolder.getAbsolutePath());
File[]files = dataFolder.listFiles();
for(File f:files){...}
This code crashes on the for(File f:files)
The code is accessing a folder on my flash drive and I checked the path and its fine.
The output of the println staments is shown below: The first set is for the IDE and the second set for the command line:
IDE output:
data files is folder: true
data files is file: false
data files: [Ljava.io.File;@a298b7
data folder: F:\JambTextProject\Saved Images
data folder path : F:\JambTextProject\Saved Images
Command line output(obtained by running java -jar jarName.jar):
data files is folder: false
data files is file: false
data files: null
data folder: F:\JambTextProject\dist\Saved Images
data folder path : F:\JambTextProject\dist\Saved Images
So what could it be? Why does the code run well in the IDE, but it crashes when run from the jar? The flash drive has 391 MB free space and the size of the folder to be accessed is 163 MB while its size on disk is 292 MB