-3

Hi guys I have written a code which will output the contents from a xml files located in different folders into a csv file. But when there are no xml files, I would like to return a print statement with "No files" found.

The issue is my code just refuses to go into the main if statement and always executes the else statement.

Below is my code.

public class XMLReadExtract {

    public void extract_XML_Data(String InputPath, File OutputPath, FileWriter fileWriter) throws Exception {

        String COMMA_DELIMITER = ",";                                                   /* Delimiter used for csv */

        File root = new File(InputPath);
        File[] list = root.listFiles();                                                 /* To List all files in the Input Path */


        try{        
            if (list == null){
                System.out.println("No files to be read");
                return;
            }                                                                               // End of main If Statement
Vishvesh Savant
  • 105
  • 1
  • 2
  • 11
  • "why the java code does not go into the if statement?" - Well, this has usually something to do with the condition .... – sinclair Sep 12 '17 at 17:59
  • well you need to ask your question **`clear`**, and send the part of the code that have the problem, not all of the code ! – Mohsen_Fatemi Sep 12 '17 at 18:01
  • 1
    Please reduce this to a [mcve]. You've posted over 100 lines of code, most of which is irrelevant to the problem. Next, the documentation for `File.listFiles` explicitly says it will return a 0-element array if no files are found - it only returns null if the path doesn't exist or isn't a directory. – Jon Skeet Sep 12 '17 at 18:01
  • [How can I check whether an array is null?](https://stackoverflow.com/questions/2369967/how-we-check-for-null-array-in-java) – Bernhard Barker Sep 12 '17 at 18:13

1 Answers1

1

You should check your condition:

if (list == null || list.length == 0) {
Christopher
  • 9,682
  • 7
  • 47
  • 76