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) {
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) {
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);
}
These method used to work in my first version without any problem and now they are suddenly throwing null pointer exception when I am rewriting the code and was doing unit testing for these methods.
Exception in thread "main" java.lang.NullPointerException at readandcopyupdate1.ReadAndCopyUpdate1.listFilesForOldFolder(ReadAndCopyUpdate1.java:33) at readandcopyupdate1.ReadAndCopyUpdate1.main(ReadAndCopyUpdate1.java:15)
What I am trying to accomplish is the following: 1. folder1 consist of files which have 33 characters in their file name. I want to pick all those files which have number 9 in their 33th position.
1-I'll store all such file names in an arrayList 'oldFiles'
2-I'll scan folder2 and store all files there in arrayList 'newFiles'
3-I am testing them by printing arrayLists values on sceen.
Last updated:11/13/2014 12:17:10 AM
David Miller
You are getting a null-pointer exception, because you have allocated the space for 100 array elements, but you still need to initialize them:
So before accessing a1[i].text you need to initialize it by calling a1[i] = new Array()
Also I am quite sure, that you actually wanted to create some other kind of object, not Array. Array the class you are currently writing, as I understand, so you probably want to have multiple Strings, e.g. String[].