请教这怎么解决?找了好久都没找着有关问题
请问这如何解决??找了好久都没找着问题
Exception in thread "main" java.lang.NullPointerException
at CodeCounter.main(CodeCounter.java:22)
这是一个代码结算的小程序,明明fa有赋值阿,怎么会是空指针呢
------解决方案--------------------
f.listFiles() 可以返回为null的,最好做一下判断。
if(fa==null){
return;
}
具体的解释是,有人说是隐藏文件啥,我这情况我也遇到过,是什么原因,不太想追究。
------解决方案--------------------
是文件夹权限的问题,与隐藏无关
Exception in thread "main" java.lang.NullPointerException
at CodeCounter.main(CodeCounter.java:22)
这是一个代码结算的小程序,明明fa有赋值阿,怎么会是空指针呢
- Java code
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CodeCounter { static int blankLine; //空白行 static int commentLine; //注释行 static int validLine; //有效行 static Pattern pBlank = Pattern.compile("^[\\s[^\\n]]*$"); public static void main(String[] args) { File f =new File("E:\\Eclipse\\Workspace\\RegExp\\src\\"); File[] fa = f.listFiles(); BufferedReader br = null;; String line = null; for(File child : fa) { if(child.getName().matches(".*\\.java$")) { try { br = new BufferedReader(new FileReader(child)); while((line=br.readLine())!=null) { parse(line); br.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println("blankLine: " + blankLine); System.out.println("commentLine: " + commentLine); System.out.println("validLine: " + validLine); } private static void parse(String line) { boolean flag = false; if(line.matches(pBlank.pattern())) { blankLine++; }else if(line.startsWith("/*")&&!line.endsWith("*/")) { commentLine++; flag = true; }else if(flag==true) { commentLine++; if(line.endsWith("*/")) flag = false; }else validLine++; } }
------解决方案--------------------
f.listFiles() 可以返回为null的,最好做一下判断。
if(fa==null){
return;
}
具体的解释是,有人说是隐藏文件啥,我这情况我也遇到过,是什么原因,不太想追究。
------解决方案--------------------
是文件夹权限的问题,与隐藏无关