IndexOutOfBoundsException in File array. What could be the issue?

So I have a directory in my local C drive.

C:/Search Files/Folder [number]/hello.txt Inside Search Files I have four foldes named: Folder 1 Folder 2 Folder 3 Folder 4

Inside Folder 1 I have a a file called hello.txt with some String in it.

What I want to do is grab the fileDirectory, fileName and fileContent and put it in a List of XMLMessage objects. I have pasted my main class and my XMLMessage POJO. When I run it, I am getting an indexOutOfBoundsException. I have been stuck for a couple hours now. I need another pair of eyes to look into this.

Thanks,

package org.raghav.stuff;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class GetFile {

public static void main(String[] args) throws IOException {
    File[] files = new File("C:\\Search Files").listFiles();

    showFiles(files);
}

public static void showFiles(File[] files) throws IOException {
    String line = null;
    List<XMLMessage> xmlMessageList = new ArrayList<XMLMessage>();

    int i = 0;
    //XMLMessage folderFile = new XMLMessage();
    try {
        for (File file : files) {
            if (file.isDirectory()) {
                String fileName = file.getName();
                System.out.print(fileName);
                xmlMessageList.get(i).setFileName(fileName);

                //folderFile.setFileName(fileName);
                showFiles(file.listFiles()); // Calls same method again.
            } else {

                xmlMessageList.get(i).setFileDirectory(file.getName() + file.toString());
                //folderFile.setFileDirectory(file.getName() + file.toString());
                System.out.print("\tFile: " + file.getName()
                        + file.toString());
                // System.out.println("Directory: " + file.getName());
                BufferedReader in = new BufferedReader(new FileReader(file));
                while ((line = in.readLine()) != null) {
                    xmlMessageList.get(i).setFileContent(line);
                //  folderFile.setFileContent(line);
                    System.out.print("\t Content:" + line);
                }
                in.close();
                System.out.println();
            }
            i++;
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    System.out.println(xmlMessageList.toString());
}
}

Here is the POJO:

package org.raghav.stuff;

public class XMLMessage {

private String fileDirectory;
private String fileName;
private String fileContent;

public final String FILE_NAME = "fileName";
public final String FILE_DIRECTORY = "fileDirectory";



public XMLMessage(String fileDirectory, String fileName, String fileContent) {
    this.fileDirectory = fileDirectory;
    this.fileName = fileName;
    this.fileContent = fileContent;
}

public XMLMessage() {

}

public String getFileDirectory() {
    return fileDirectory;
}

public void setFileDirectory(String fileDirectory) {
    this.fileDirectory = fileDirectory;
}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public String getFileContent() {
    return fileContent;
}

public void setFileContent(String fileContent) {
    this.fileContent = fileContent;
}

public String toString(){
    String returnString = "File Directory: " + fileDirectory + "\n" + "File Name" + fileName + "\n" + "File Content: " + fileContent;
    return returnString;
}

/*public String createResponseFileName(String fileName){

    int lastDot = fileName.lastIndexOf('.');
    String responseFileName = fileName.substring(0, lastDot) + "Response" + fileName.substring(lastDot);    
    return responseFileName;

}*/

/*public String createResponseFileContent(String fileContent){
    this.
}*/
}
Jon Skeet
people
quotationmark

You're never populating your list. I suspect you should actually have:

for (File file : files) {
    XMLMessage message = new XMLMessage();
    xmlMessageList.add(message);
    if (file.isDirectory()) {
        String fileName = file.getName();
        System.out.print(fileName);
        message.setFileName(fileName);

        //folderFile.setFileName(fileName);
        showFiles(file.listFiles()); // Calls same method again.
    } else {
        ... etc, using message instead of xmlMessageList.get(i)
    }
}

Then you don't need the i variable at all.

people

See more on this question at Stackoverflow