日期:2014-05-20  浏览次数:20957 次

用Java写的遍历目录下面文件,总是出错
Java code
public class traverse {
    public static void tra(File path){
        if(path == null){
            return;
        }
        if(path.isDirectory()){
            String[] files = path.list();
            for(int i = 0; i < files.length;i++){
                tra(new File(path,files[i]));
            }
        }else{
            if(path.getAbsolutePath().endsWith(".exe"))
                System.out.println(path);
        }
    }
    public static void main(String[] args) {
        try{
        File file = new File("D:\\");
        tra(file);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}


老是抛出异常
java.lang.NullPointerException
请问这是为什么啊

------解决方案--------------------
探讨
Java code
public class traverse {
public static void tra(File path){
if(path == null){
return;
}
if(path.isDirectory()){
String[] files = path.list……