-1

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

gbenroscience
  • 994
  • 2
  • 10
  • 33
  • You should read the javadoc for `File.listFiles`. – Andy Turner Oct 12 '15 at 16:55
  • And it isn't the listFiles method which throws the NPE, it is you trying to iterate the result in the enhanced for loop. – Andy Turner Oct 12 '15 at 16:56
  • The question is not about the NullPointer, the question is why the code behaves differently in the IDE and on the commandline – gbenroscience Oct 12 '15 at 16:58
  • Take a look at this: http://stackoverflow.com/questions/16239130/java-user-dir-property-what-exactly-it-means – Titus Oct 12 '15 at 17:13
  • Thanks, but I know exactly what user.dir means and I've written apps that leverage it. Or am I missing something? please explain? – gbenroscience Oct 12 '15 at 17:27
  • 1
    The `jar` file is in the `dist` folder but your code from the IDE is executed directly from `JambTextProject`, at least that is what it seems like from the paths that your program outputs `F:\JambTextProject\Saved Images` and `F:\JambTextProject\dist\Saved Images` – Titus Oct 12 '15 at 17:32
  • I just discovered that too now when I examined the FIle.list() output. Thanks Titus, that actually solved it, too, alongside Andy Turner's answer. Your answer was the particular answer to Andy Turner's more general one. – gbenroscience Oct 12 '15 at 17:43

1 Answers1

1

According to the javadoc of File.listFiles, the method:

Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

So when you try to iterate the result, it is null, and a NullPointerException occurs.

This implies that your IDE is set up differently to your command line, e.g. it has a different working directory.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • So why doesn't the IO error occur when I run the code directly? why does it occur only when I double click the jar or run it from the command line. Thanks, but I already know why the NullPointerException occurs. – gbenroscience Oct 12 '15 at 17:02
  • Actually, the IDE also runs the project from the same flash drive that the command line runs it from. So can the working directories still be different under these circumstances – gbenroscience Oct 12 '15 at 17:13