0

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.

Mohsin Ali
  • 125
  • 1
  • 12
  • sorry what d you mean by stack trace? – Mohsin Ali Dec 26 '14 at 18:54
  • What is the exact error you are getting? Can you [print the stack trace](http://stackoverflow.com/a/6823007/1492578) of the exception? – John Bupit Dec 26 '14 at 18:57
  • it is printing the exception given in my question. e.printstack is printing the exception: Exception caught.java.lang.NullPointerException – Mohsin Ali Dec 26 '14 at 19:00
  • Mohsin, the stack trace is the entire chain of calls that lead to the exception. @John is asking you about it because it contains a lot of useful information to help us understand your issue. Read this [post](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) if you still don't know what we are talking about – ulix Dec 26 '14 at 19:10
  • I think the problem lies in the calling of recursion function. – Mohsin Ali Dec 27 '14 at 04:31

1 Answers1

0

Method compare_With_TreeFolder may return null. The following line would then try to call method toString on null, which is an excellent reason for a NPE.

matchedfileTextArea.setText(obj.compare_With_TreeFolder(redFile, redFolder).toString());

I'm not sure what the return value null should indicate, but it must be checked before using toString.

Later

Some more improvements - see XXX

System.out.println("Exception caught." + e);
e.printStackTrace();   // XXX Add this to learn about the location of an Exception

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(redFolder, str);    // XXX prefix the file with the folder!
    if (file_Folder.isDirectory()) {
      // XXX if the recursive call finds a match: return the File
      File res = compare_With_TreeFolder(redFile, file_Folder);
      if( res != null ) return res;
    } else if (file_Folder.isFile()) {
      if (redFile.getName().equals(file_Folder.getName())) {
        if (redFile.length() == file_Folder.length()) {
          if (compareFile(redFile, file_Folder) == 1) {
            return file_Folder;
          }
        }
      }
    }
  }
  return null;
}

I haven'T found any other reason for a NPE. I suggest that you add the printStackTrace and report back.

laune
  • 31,114
  • 3
  • 29
  • 42
  • Dears, I have checked without using toString(), even then it is displaying this in output area: run: Exception caught.java.lang.NullPointerException whenever I make selection on a folder on the tree the same error is displayed. – Mohsin Ali Dec 27 '14 at 04:21