package AmendClass;import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Amend {
private File f;
String str;
Amend(File f){
this.f=f;
}
public void AmendSource(){
File file[]=f.listFiles();
for(int i=0;i<=file.length;i++){
String regex=".*\\d{n,}.*";
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(file[i].getName());
while(m.find()){
String str1=m.group();
file[i].renameTo(new File(str1));
}

}
}}package AmendClass;import java.io.File;public class Test { public static void main(String[] args) {
File f=new File("D/11Download");
Amend a=new Amend(f);
a.AmendSource();
}}求问为什么会有空指针异常。

解决方案 »

  1.   

    先不说结果怎么样,我觉得java中的方法应该小写字母大头!是吧
      

  2.   

    new File("D/11Download");
    这个括号里面的文件路径你是不是写错了,你改了再试一试。
      

  3.   

    在遍历文件时先判断是否为空,如下
    if (file!=null) {
    for (int i = 0; i <= file.length; i++) {
    String regex = ".*\\d{n,}.*";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(file[i].getName());
    while (m.find()) {
    String str1 = m.group();
    file[i].renameTo(new File(str1));
    } }
    }
      

  4.   


    new File("D:/11Download");
    中间差个“:”