下面的程序统计小一点的文件,没问题.
可是统计整个D盘,就不行了,出现空指针异常...
Exception in thread "main" java.lang.NullPointerException
at CodeCounter.tree(TestCodeCounter.java:15)
at CodeCounter.tree(TestCodeCounter.java:19)
at CodeCounter.<init>(TestCodeCounter.java:10)
at TestCodeCounter.main(TestCodeCounter.java:74)我真的没辙了!!求助下CSDN的朋友,看看那里需要该该呢?
十分感谢!!import java.io.*;class CodeCounter {
private static long normalLines = 0; //普通行
private static long commentLines = 0; //注释行
private static long whiteLines = 0;  //空白行

public CodeCounter(String s) {
File f = new File(s);
tree(f);
}

public void tree(File f) {
File[] childs = f.listFiles();
for(int i = 0; i < childs.length; i++) {
if(childs[i].getName().matches(".*\\.java$")) {
parse(childs[i]);
}else if(childs[i].isDirectory()) {
tree(childs[i]);
}
}
} public void parse(File f) {
BufferedReader br = null;
boolean comment = false;
try {
br = new BufferedReader(new FileReader(f));
String line = "";
while((line = br.readLine()) != null) {
line = line.trim();
if(line.matches("^[\\s&&[^\\n]]*$")) {
whiteLines ++;
}else if(line.startsWith("/*") && !line.endsWith("*/")) {
commentLines ++;
comment = true;
}else if(true == comment) {
commentLines ++;
if(line.endsWith("*/")) {
comment = false;
}
}else if(line.startsWith("//")) {
commentLines ++;
}else {
normalLines ++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}

} public void p() {
System.out.println("normalLines: " + normalLines);
System.out.println("commentLines: " + commentLines);
System.out.println("whiteLines: " + whiteLines);
}}
public class TestCodeCounter {
public static void main(String[] args) {
         //CodeCounter c = new CodeCounter("D:\\java"); //统计这个文件夹没问题
CodeCounter c = new CodeCounter("D:\\"); //整个D盘,就over了!!
c.p();
}
}

解决方案 »

  1.   

    我前几天在下面的这张帖里贴过一个,你可以去参考一下。http://topic.csdn.net/u/20080110/14/7ca0e8ec-18ff-410f-8300-32540b4e905d.html
      

  2.   

    File不是针对文件的吗,可能你把
    public CodeCounter(String s) {
            File f = new File(s);
            tree(f);
        }
    这段修改一下看看,现在我没时间帮你调了,自己baidu查一下试试吧
      

  3.   

    有可能是跟文件夹的权限有关系
    你用的是fat32么?
    不行就捕获一下,把异常处理了.
      

  4.   

    我的那些个代码你可能要根据你的要求自行改一下吧,Java中的注释挺麻烦的。我的那个代码能处理的注释符号,如://、/*等不能出现代码中的字符串里(这个处理还没有完成)。你的代码的错误好像在tree()方法中。
      

  5.   

    CodeCounter c = new CodeCounter("D:");
    就可以了你试一下
      

  6.   

    "CodeCounter   c   =   new   CodeCounter("D:"); 
    就可以了你试一下"
    D:,这样不行呀,直接就
    normalLines: 0
    commentLines: 0
    whiteLines: 0我的D盘是NTFS的开始的时候,程序在运行(能听见硬盘响声,大约半分钟,就抛异常了)
      

  7.   

    File[] childs = f.listFiles();
            for(int i = 0; i < childs.length; i++) {
                if(childs[i].getName().matches(".*\\.java$")) {
                    parse(childs[i]);
                }else if(childs[i].isDirectory()) {
                    tree(childs[i]);
                }
            }
    我想起来了 当 有个空文件夹的时候
    File[] childs = f.listFiles();返回为空
    所以有 控指针异常 判断一下
      

  8.   

    回复:sun_3211
    空文件夹怎么判断的呀?
    我下面弄的不对呀!
    结果:
    normalLines: 0
    commentLines: 0
    whiteLines: 0
    public void tree(File f) {
    File[] childs = f.listFiles();
    for(int i = 0; i < childs.length; i++) {
    if((childs[i].isFile()) && childs[i].getName().matches(".*\\.java$")) {
    parse(childs[i]);
    }else if((childs[i].length() != 0) && childs[i].isDirectory()) {
    tree(childs[i]);
    }
    }
    }
      

  9.   

    回复:sun_3211 
    刚才试了下,"空指针异常"确实是空文件夹引起的呀!!
    可是空文件夹怎么排除呀?
    我上面写的那个不对呢?
      

  10.   

    把tree的判断简单修改了一下,不用判断什么目录空不空,for循环的时候如果没有内容就会调过的。    public void tree(File f) {
         System.out.println("Path : "+f.getPath());
            File[] childs = f.listFiles();
            for(int i = 0; i < childs.length; i++) {//这里决定了空目录不会执行
                if(childs[i].isDirectory()){//这里决定什么是目录
                    tree(childs[i]);
                }else if(childs[i].getName().matches(".*\\.java$")) {//原来这个判断是在前面的,如果正好有个.java的目录你就完蛋了
                    parse(childs[i]);
                }
            }
        }
      

  11.   

    上面打错个字“for循环的时候如果没有内容就会过的。”
      

  12.   

    回复:wangqiyy 
    是这个文件过不去的,"System Volume Information".D:\System Volume Information我把它排除了,这次过去了呀.哈哈,你的这句话真管用呀(System.out.println("Path   :   "+f.getPath());)!!学习了..
    public void tree(File f) {
    System.out.println("Path   :   " + f.getPath());
    File[] childs = f.listFiles();
    for(int i = 0; i < childs.length; i++) {
    if(childs[i].isDirectory()) {
    if(childs[i].getName().equals("System Volume Information")) {
    continue;
    }else {
    tree(childs[i]);
    }

    }else if(childs[i].getName().matches(".*\\.java$")) {
    parse(childs[i]);
    }
    }
    }
      

  13.   

    if(childs ==null){
        return;
    }
    有了这句话在
    File[] childs = f.listFiles();
    后面不就可以了
      

  14.   

    sun_3211的方法好呀!!
    嘿嘿,我那个equals效率太差了