本帖最后由 harborbest 于 2013-4-27 21:23 编辑
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)
大家,知道,这是为什么吗?谢谢了 |