# Thread: counting files in folder

1. Member
Join Date
Dec 2012
Posts
42
Rep Power
0

## counting files in folder

Hi, I am trying to count files in a folder and I keep getting java.lang.NullPointerException

Java Code:
String path = new File("Images").getAbsolutePath();
System.out.println(path);

int rows = new File(path).list().length;
output of system.out.println is:
c:\path\to\folder\Images
and then I think because of the .length I get that NullPointerException.

Can someone see what I am doing wrong here? Am I doing something wrong when getting the path to this folder?

2. ## Re: counting files in folder

Before you do anything else, print out what that list() method returns.

kind regards,

Jos

3. Member
Join Date
Dec 2012
Posts
42
Rep Power
0

## Re: counting files in folder

Java Code:
String[] test = new File(path).list();
System.out.println(test);
output = null

This means that the path is wrong somehow? It for sure leads to the correct folder with the images.

if I do it like this:

Java Code:
String path = new File("").getAbsolutePath();
System.out.println(path);

String[] test = new File(path).list();
System.out.println(test);
I get this:

c:\path\to\folder
[Ljava.lang.String;@3ee37fa6
and this is correct, so how can I add a folder to that path?
Last edited by Gatsu; 12-08-2012 at 10:31 PM.

4. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
13

## Re: counting files in folder

What is really your main question was? Is it counting files in a folder or adding a folder to a path?

5. ## Re: counting files in folder

And note that you can count files in a folder -- you can't count files in a file that isn't a folder.

db

6. Member
Join Date
Dec 2012
Posts
42
Rep Power
0

## Re: counting files in folder

What is really your main question was? Is it counting files in a folder or adding a folder to a path?
I still want to count files in a folder but I think there is a problem with how I reach my folder that I want to count my files in so the thread escalated into where my problem really lies, and its in the path!
Last edited by Gatsu; 12-09-2012 at 02:17 PM.

7. Member
Join Date
Dec 2012
Posts
42
Rep Power
0

## Re: counting files in folder

Originally Posted by DarrylBurke
And note that you can count files in a folder -- you can't count files in a file that isn't a folder.

db
"Images" is not a file, it is a folder.

8. Member
Join Date
Dec 2012
Posts
42
Rep Power
0

## Re: counting files in folder

no wait, when I use this "System.getProperty("user.dir");" I get the correct folder, thank you all, problem solved =) love you very much!

9. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
13

## Re: counting files in folder

To create a file object that represent your path you can use something like this:

Java Code:
File file = new File("C:\\Images");
To get the number of files in that folder you can use the listFiles() method which return you an array of File. Iterate this array, make sure that this file object is representing a file and not a directory using the isFile() or isDirectory() method.

10. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
13

## Re: counting files in folder

Originally Posted by Gatsu
no wait, when I use this "System.getProperty("user.dir");" I get the correct folder, thank you all, problem solved =) love you very much!
Using the "user.dir" property will give you your current working directory. Well, if you need to get your current working directory you can use it.

11. Member
Join Date
Dec 2012
Posts
42
Rep Power
0

## Re: counting files in folder

To create a file object that represent your path you can use something like this:

Java Code:
File file = new File("C:\\Images");
To get the number of files in that folder you can use the listFiles() method which return you an array of File. Iterate this array, make sure that this file object is representing a file and not a directory using the isFile() or isDirectory() method.
Thank you, I needed this info

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•