sotI
sotI

Reputation: 63

unknown source of 'java.lang.NullPointerException' error

import java.io.File;


public class filesinDirectory {

public static void main(String[] args) {
    File path = new File("/home/vito/Desktop/Dir1");
    File[] files = path.listFiles();
    int length = files.length; // line 9

    for (int i = 0; i != length; i++) {
        if (files[i].isFile()) 
        System.out.println( i + ": " + files[i].getName());
    }
}
}

With the above code, every time I try to run it I get a java.lang.NullPointerException error on line 9. However I am not sure what the source of this error is.

Upvotes: 1

Views: 10216

Answers (5)

mkro
mkro

Reputation: 1892

Javadoc says:

File.listFiles()

Returns:
    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 null if this abstract pathname does not denote a directory, or 

if an I/O error occurs.

So either the directory does not exist or an I/O error occured.

Upvotes: 0

c00kiemon5ter
c00kiemon5ter

Reputation: 17654

Well, lets see what the javadoc says

public File[] listFiles()
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned, one for each file or directory in the directory. Pathnames denoting the directory itself and the directory's parent directory are not included in the result. Each resulting abstract pathname is constructed from this abstract pathname using the File(File, String) constructor. Therefore if this pathname is absolute then each resulting pathname is absolute; if this pathname is relative then each resulting pathname will be relative to the same directory.

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Returns:
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 null if this abstract pathname does not denote a directory, or if an I/O error occurs. Throws:
SecurityException - If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the directory Since: 1.2

Upvotes: 0

duffymo
duffymo

Reputation: 308938

According to the javadocs for java.io.File:

Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

Must be that there's a problem with the method call before.

Upvotes: 0

Brendan Long
Brendan Long

Reputation: 54262

According to the Javadocs for File.listFiles():

If this abstract pathname does not denote a directory, then this method returns null.

So, Java is saying that /home/vito/Desktop/Dir1 isn't a valid directory. Do you have your capitalization wrong or something?

Upvotes: 5

Mike Thomsen
Mike Thomsen

Reputation: 37506

files is null. Therefore you're getting a NPE when you call its length property.

Upvotes: 3

Related Questions