I am trying to write a small app that will recursivly add all files and files in sub directories to a ArrayList. The code I am using is more or less a cut and paste taken from this post:
http://forum.java.sun.com/thread.jspa?threadID=674245&messageID=3938703
I declare all my variables publicly. Not sure if that was the best idea, but it allows me to access them easily from non static methods. This also isn't the complete code. The app accepts arguments from the command line, and checks them for validity (ex. is the user entered starting directory a directory)
public class AllFiles {
public File fnfArray[]=null, currentFile=null, source; //Files and Folders array
public List fnfList; //Files and Folders List
public ArrayList list, deeperList;
public Iterator filesIter;
public static void main (String args[]){
AllFiles all=new AllFiles();
all.source=args[0] //this isn't how i do it, pretend it sets source to a directory.
all.getFiles(all.source);
public ArrayList getFiles(File startDir)
{
list=new ArrayList();
fnfArray=startDir.listFiles();
fnfList=Arrays.asList(fnfArray);
filesIter=fnfList.iterator();
while(filesIter.hasNext()){
currentFile=(File)filesIter.next();
currentFile=currentFile.getAbsoluteFile()
list.add(currentFile);
if(currentFile.isDirectory()){
deeperList=getFiles(currentFile);
list.addAll(deeperList);
}
}
Collections.sort(list);
return list;
}
}
The two reason I am writing is:
1) this compiles but erros.
2) here is the error:
Exception in thread "main" java.lang.NullPointerException
at java.util.Arrays$ArrayList.<init>(Arrays.java:2342)
at java.util.Arrays.asList(Arrays.java:2328)
at hogan.getFiles(hogan.java:34)
at hogan.getFiles(hogan.java:41)
at hogan.main(hogan.java:26)
I read something about Arrays.java.2342/2328 being a bug in Java, but it always seemed to be related to web development. Any ideas. Thanks a ton, I am extremly new to programming, and it was this forum that got me this far.
HippieJoe