import java.io.*;
public class FileList
{
public static int fileTotalNum = 0; //文件总数
public static int directoryNum = 0; //文件夹总数
public void FList(File f)
{
String[] s = f.list();
fileTotalNum+=s.length;
for(int i = 0;i < s.length;i++)
{
File filelist = new File(f.getPath()+s);
if(filelist.isDirectory())
{
directoryNum++;
FList(filelist);
}
}
}
public static void main(String[] args)
{
File file = new File("C:\\");
FileList fl = new FileList();
fl.FList(file);
System.out.println("一共有文件:"+fileTotalNum);
}
}
执行以上代码后提示空指针错误,具体如下:
Exception in thread "main" java.lang.NullPointerException
at FileList.FList(FileList.java:10)
at FileList.FList(FileList.java:19)
at FileList.main(FileList.java:28)