-1

I don't quite understand what's going wrong. This code worked before but after I moved around some directories (despite my having moved them back to how it was before), now all of a sudden the code refuses to compile calling it a NullPointerException (despite the fact that I'm creating a new object right there).

cat is a string. In the currently existing instance cat is passed as "cat1" which is the name of a directory in my project folder.

for(int i = 1; i <= new File(cat).listFiles().length; i++){

Stack Trace listed below:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$99(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at sample.Category.CreateCatLayout(Category.java:49)
    at sample.Category.<init>(Category.java:41)
    at sample.Main.start(Main.java:71)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$106(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$119(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$117(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$118(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$450(GtkApplication.java:139)
    ... 1 more
Exception running application sample.Main
Ben Boudreau
  • 25
  • 1
  • 6
  • 3
    *the code refuses to compile calling it a NullPointerException* - NPE is a runtime error, not a compiler error. Where's your code? Where's the stack trace? – shmosel Aug 16 '17 at 03:37
  • 1
    As per the javadocs of `listFiles()`: `An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns "null" if this abstract pathname does not denote a directory, or if an I/O error occurs.` Maybe the the value of `cat` doesn't represent a directory. That should be a good place to start. – Krishnan Mahadevan Aug 16 '17 at 03:42
  • I put only this line of code because everything from the error messages all point to this one line of code. I will try to edit in the stack trace. – Ben Boudreau Aug 16 '17 at 03:42
  • Is it a common that creating new objects continually in the termination condition of a for statement, though this is not the main question? – JeongjinKim Aug 16 '17 at 03:43
  • @BenBoudreau you must share the stacktrace of the exception still for better clarity. – Naman Aug 16 '17 at 03:45

2 Answers2

1

You are ignoring the possibility that File.listFiles() returns null. It's documented.

for(int i = 1; i <= new File(cat).listFiles().length; i++){

This is an atrocious piece of code in the first place. Every time you iterate you are re-reading the directory via listFiles(), which is a massive overhead, and you must be calling listFiles() again inside the loop, to make any use of i, so it is two directory lists per iteration. And the risk of the directory changing underneath you between calles. And you are iterating one too many times.

It should be:

File[] files = new File(cat).listFiles();
if (files != null)
{
    for (int i = 1; i < files.length; i++)
        // do something with files[i]
}

or better still use:

    for (File file : files)
        // do something with file
user207421
  • 305,947
  • 44
  • 307
  • 483
0

The code used above can throw a NPE at runtime(as mentioned by @shmosel already) as the listFiles() can return a null value as in its implementation:

public File[] listFiles() {
    String[] ss = list();
    if (ss == null) return null;
    ...
}

Its javadoc here also supports the same.

So if the directory path(pathname cat in your code) does not have any files/directories within it, then the listFiles call would return null and the .length over it would throw a NullPointerException.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • Okay so that definitely means this needs to be done better. The base of my problem here is that it was working before and suddenly stopped working, but this in and of itself is probably poor programming practice. – Ben Boudreau Aug 16 '17 at 03:49
  • @BenBoudreau Indeed, you should probably check even if its a directory you're working on using [isDirectory()](https://docs.oracle.com/javase/7/docs/api/java/io/File.html#isDirectory()) and then try to use listFiles probably. – Naman Aug 16 '17 at 03:51
  • Thanks for that tip. Turns out using isDirectory shows that it is 'false'. Need to figure out where to go from here. – Ben Boudreau Aug 16 '17 at 03:55
  • 1
    @BenBoudreau That would indicate that `cat` isn't a directory, or doesn't exist at all, or that you don't have read access to it. – user207421 Aug 16 '17 at 03:56
  • *isDirectory shows that it is 'false'* that pretty much states why you're getting an NPE in the code you'd shared. – Naman Aug 16 '17 at 03:56
  • 1
    Figured out my stupid problem (as they tend to be). Was passing "cat0", not "cat1" for cat as I thought because I set up a different for loop incorrectly. Thanks for all the help guys. – Ben Boudreau Aug 16 '17 at 04:02