import java.io.File;
public class filesinDirectory {
public static void main(String[] args) {
File path = new File("/home/vito/Desktop/Dir1");
File[] files = path.listFiles();
int length = files.length; // line 9
for (int i = 0; i != length; i++) {
if (files[i].isFile())
System.out.println( i + ": " + files[i].getName());
}
}
}
With the above code, every time I try to run it I get a java.lang.NullPointerException error on line 9. However I am not sure what the source of this error is.
Javadoc says:
if an I/O error occurs.
So either the directory does not exist or an I/O error occured.