OiRc
OiRc

Reputation: 1622

iterate on file inside a folder

i found this solution to iterate files inside a folder,but it gave me nullpointException

import java.io.File;

public class Main {
    public static void main(String[] args) {
    File path = new File("/resources/subFolder"); 
    File [] files = path.listFiles(); -> line of nullPointExpection
    for (int i = 0; i < files.length; i++){
        if (files[i].isFile()){ 
            System.out.println(files[i]);
        }
    }
}
}

this folder is inside an eclipse project.

project
  -src
  -resources
    -subFolder
      -file1
      -file2

Exception in thread "main" java.lang.NullPointerException
    at Main.main(Main.java:9)

any suggest to resolve it?

Upvotes: 0

Views: 97

Answers (5)

Asif Bhutto
Asif Bhutto

Reputation: 3994

try this,

public class Main {
    public static void main(String[] args) throws URISyntaxException {

        java.net.URL dirURL = ClassLoader.getSystemResource("resources");
        File[] filePaths = new File(dirURL.toURI()).listFiles();
        for (File file : filePaths) {
            if (file.isFile()) {
                System.out.println(file);
            }
        }

    }
}

Upvotes: 0

commesan
commesan

Reputation: 526

Try changing your path from:

File path = new File("/resources/subFolder"); 

to

File path = new File("resources/subFolder"); 

or perhaps

File path = new File("../resources/subFolder"); 

Depending on your configuration.

Your code with the added suggestion from @Roberto runs without any nullpointerexception

import java.io.File;

public class Main {
    public static void main(String[] args) {
        File path = new File("/home/sander"); 
        if(path.exists()) {
            File [] files = path.listFiles(); 
            for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()){ 
                System.out.println(files[i]);
            }
            }
        } else {
            System.out.println("Path doesn't exist");
        }
    }
}

If this doesn't work for you, perhaps it could it be a file permissions issue (unlikely as it may seem).

Upvotes: 0

Damian
Damian

Reputation: 550

If

File [] files = path.listFiles()

returns NullPointerException it means that path object is null. It means that

File path = new File("/resources/subFolder");

created null object what is quite abnormal situation in Java. Check if line number 9 refers to the line you provided. I guess that NullPointerException is caused by the loop

for (int i = 0; i < files.length; i++){

because you don't have files in given directory or your path is incorrect as Roberto subbested.

Upvotes: 0

jommy
jommy

Reputation: 11

The javadoc from File.listFiles() says:

 * @return  An array of abstract pathnames denoting the files and
 *          directories in the directory denoted by this abstract pathname.
 *          The array will be empty if the directory is empty.  Returns
 *          {@code null} if this abstract pathname does not denote a
 *          directory, or if an I/O error occurs.

So I would assume directory does not exists, most likely because of the preceding '/' character in path, which basically means it starts searching from the root of the drive, i.e. using absolute path.

Upvotes: 0

Roberto
Roberto

Reputation: 520

The NPE is caused by /resources/subFolder not existing.

The problem is the leading / in /resources/subFolder. This means an absolute path, i.e. relative to the root of your filesystem. You should remove it to make it a relative path, i.e. resources/subFolder.

A good idea would be to use File.exists() before the loop.

Upvotes: 3

Related Questions