The program's objective is to print all the directories, sub-directories and files from the folder that we specify on the Client side. This program does that, but in the very end, it throws a NullPointerException. I use a recursive function to print the sub-dirs.
The error is
Exception in thread "main" java.lang.NullPointerException at Client.main(Client.java:114)
Here is the Client-side program which deals with the listing
try{
File maindir = new File(folder);
if(maindir.exists() && maindir.isDirectory())
{
// array for files and sub-directories
// of directory pointed by maindir
File arr[] = maindir.listFiles();
System.out.println("Files from main directory : " + maindir);
// Calling recursive method
RecursivePrint(arr, 0);
}
}
catch (NullPointerException e) {
System.out.println("NPE caught");
}
Here is RecursivePrint
static void RecursivePrint(File[] arr, int level)
{
// for-each loop for main directory files
for (File f : arr)
{
// tabs for internal levels
for (int i = 0; i < level; i++)
System.out.print("\t");
if(f.isFile())
System.out.println(f.getName());
else if(f.isDirectory())
{
System.out.println("[" + f.getName() + "]");
// recursion for sub-directories
RecursivePrint(f.listFiles(), level + 1);
}
}
}
Can someone help me figure out why the NullPointerException occurs and how to resolve it?