为什么我得到空指针异常

时间:2014-10-24 07:36:00

标签: java arraylist

//Author: Darin Park
//Date: 24 October, 2014
//Version: 2
package readandcopyupdate1;

import java.util.ArrayList;
import java.io.*;

public class ReadAndCopyUpdate1{

public static void main(String[] args){
    ReadAndCopyUpdate1 rc = new ReadAndCopyUpdate1();
    final File folder1 = new File("/root/avatar/default/upload/member");
    final File folder2 = new File("/root/avatar/default/upload/Transfer");
    rc.listFilesForOldFolder(folder1,rc.oldFiles);
    rc.listFilesForNewFolder(folder2,rc.newFiles);
            rc.oldFiles.stream().forEach((oldFile) -> {
                System.out.println(oldFile);
        });

    System.out.println("\n\n");
            rc.newFiles.stream().forEach((newFile) -> {
                System.out.println(newFile);
        });


}

private void listFilesForOldFolder(final File folder, ArrayList arrayList) {
   /* 
    * This method takes two arguments. 
    * The first argument is the Original Avatar Upload Folder which we want to scan.
    * The second argument is the ArrayList where we want to store all the avatar file names.
    * The first argument is not a string, it's a File.
    * So we need to first convert string to a File by using File Method. Look below for example.
    * final File folder1 = new File("/root/avatar/default/upload/member");
   */
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            listFilesForOldFolder(fileEntry, arrayList);
        } else {
            String str = fileEntry.getName(); 
            if(str.equals("index.html")){
              continue;
            }          
            if(str.charAt(32) == '9'){
                arrayList.add(str);
            }
        }
    }
}

private void listFilesForNewFolder(final File folder, ArrayList arrayList) {
    /*
     * This method takes two arguments.
     * The first argument is the New Transfer folder where we want to store avatar's copy.
     * The second argument is the ArrayList where we want to store all the avatar names in the new Transfer folder.
     * The first argument is not a string, it's a File.
     * So we need to first convert string to a file by using File method. Look below for example.
     * final File folder2 = new File("/root/avatar/default/upload/Transfer");
    */  
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            listFilesForNewFolder(fileEntry, arrayList);
        } else {
            String str = fileEntry.getName();          
            arrayList.add(str);
        }
    }
}



private final ArrayList<String> oldFiles = new ArrayList<>(5);
private final ArrayList<String> newFiles = new ArrayList<>(5);

}

这些方法曾经在我的第一个版本中工作没有任何问题,现在当我重写代码并且正在对这些方法进行单元测试时,它们突然抛出空指针异常。

Exception in thread "main" java.lang.NullPointerException
    at readandcopyupdate1.ReadAndCopyUpdate1.listFilesForOldFolder(ReadAndCopyUpdate1.java:33)
at readandcopyupdate1.ReadAndCopyUpdate1.main(ReadAndCopyUpdate1.java:15)

我想要完成的是以下内容:  1. folder1由文件名中包含33个字符的文件组成。我想挑选所有那些在第33位排名第9的文件。

  1. 我将所有此类文件名存储在arrayList&#39; oldFiles&#39;
  2. 我将扫描folder2并将所有文件存储在arrayList&#39; newFiles&#39;
    1. 我正在通过在sceen上打印arrayLists值来测试它们。

3 个答案:

答案 0 :(得分:1)

这里,警告是:

行:“for(final File fileEntry:folder.listFiles())”

如果文件夹路径不存在,则此行可能返回null。因此在处理之前检查fileEntry变量的null。

行:“String str = fileEntry.getName();”

在使用其他值检查之前,检查此str变量的null / empty。

答案 1 :(得分:0)

测试str的长度至少为33个字符:

...
else {
    String str = fileEntry.getName();
    if(str == null || "index.html".equals(str)){
        continue;
    }          
    if(str.length() >= 33 && str.charAt(32) == '9'){
        arrayList.add(str);
    }
}
...

但是,如果确定最后一个字符是9,那么请改为:

...
else {
    String str = fileEntry.getName(); 
    if(str == null || "index.html".equals(str)){
        continue;
    }          
    if(str.charAt(str.length() - 1) == '9'){
        arrayList.add(str);
    }
}
...

答案 2 :(得分:0)

这将解决它:

String str = fileEntry.getName();
if (str == null || "index.html".equals(str)) {
    continue;
}
if (str.length() >= 33 && str.charAt(32) == '9') {
    arrayList.add(str);
}

我们错过了一件大事:

final File folder1 = new File("/home3/not_existing_follder");
if (!folder1.exists() || !folder1.isDirectory()){
    System.out.print("Folder! does not exist");
    return;
}

当文件以不存在的dir给出时,它将返回null。将它应用于两个目录,你会没事的。