I’m writing a program that uses File I/O to traverse through a directory given by the user and then adds the directories to a generic linked list. The program I wrote works perfectly on Ubuntu, but does not work when I try to use it on Windows. Its a pretty long program but this is the part that I think is having issues:
private Node<Item> currentNode = new Node<Item>();
public void traverse(File fileObject)
{
File allFiles[] = fileObject.listFiles();
for(File aFile: allFiles){
System.out.println(aFile.getName()); /* debugging */
recursiveTraversal(aFile); /* Line 34 */
}
}
public void recursiveTraversal(File fileObject){
Node<Item> newNode = new Node<Item>();
currentNode.addChild(newNode);
currentNode = newNode;
if (fileObject.isDirectory()){
newNode.setData(new Item());
File allFiles[] = fileObject.listFiles();
for(File aFile : allFiles){ /* This is line 48 */
recursiveTraversal(aFile);
}
}else if (fileObject.isFile()){
newNode.setData(new Item());
}
currentNode = newNode.getParent();
}
When I use it on Linux I can give it something like /home/matt/Documents and it works, but when I try on windows using G:\\Users\\Matt\\Documents it errors out. The print statement I threw in actually prints out files in the folder, but something with the rest of the program messes up:
java.lang.NullPointerException
at FileTraverse.recursiveTraversal(FileTraverse.java:48)
at FileTraverse.traverse(FileTraverse.java:34)
at DirectoryMain$ClickAction.actionPerformed(DirectoryMain.java:103)
...
Theres a lot of errors afterwards that have to do with the Swing GUI this program is running off of but I don’t think that has to do with anything.
EDIT: Added in line numbers that correspond with the trace.
my guess is that you are hitting a directory which for some reason windows will not allow you to view. listFiles() is most likely returning null in this instance.