I want a File be compared with each content of a folder selected using tree selection listener.I have a tree selection listener code as follows.
public void valueChanged(TreeSelectionEvent event) {
redFolder = (File) redfileTree.getLastSelectedPathComponent();
Functions obj = new Functions();
try {
if (redFolder.isDirectory()) {
matchedfileTextArea.setText(obj.compare_With_TreeFolder(redFile, redFolder).toString());
}
} catch (Exception e) {
System.out.println("Exception caught." + e);
}
}
});
The recursion method is:
//compare the selected file with the selected folder in the tree
public File compare_With_TreeFolder(File redFile, File redFolder) throws IOException, Exception {
String[] folderContents = redFolder.list();
File file_Folder;
for (String str : folderContents) {
file_Folder = new File(str);
if (file_Folder.isDirectory()) {
compare_With_TreeFolder(redFile, file_Folder);
} else if (file_Folder.isFile()) {
if (redFile.getName().equals(file_Folder.getName())) {
System.out.println(redFile.getName() + file_Folder.getName());
if (redFile.length() == file_Folder.length()) {
if (compareFile(redFile, file_Folder) == 1) {
return file_Folder;
}
}
}
}
}
return null;
}
public int compareFile(File fILE_ONE2, File fILE_TWO2) throws Exception {
File f1 = new File(fILE_ONE2.toString()); //OUTFILE
File f2 = new File(fILE_TWO2.toString()); //INPUT
FileReader fR1 = new FileReader(f1);
FileReader fR2 = new FileReader(f2);
BufferedReader reader1 = new BufferedReader(fR1);
BufferedReader reader2 = new BufferedReader(fR2);
String line1 = null;
String line2 = null;
int flag = 1;
while (true) // Continue while there are equal lines
{
line1 = reader1.readLine();
line2 = reader2.readLine();
if (line1 == null) // End of file 1
{
return (line2 == null ? 1 : 0); // Equal only if file 2 also ended
} else if (line2 == null) {
return 0; // File 2 ended before file 1, so not equal
} else if (!line1.equalsIgnoreCase(line2)) // Non-null and different lines
{
return 0;
}
}
But i'm getting this exception: Exception caught.java.lang.NullPointerException
Any help please.