Results 1 to 11 of 11
  1. #1
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: counting files in folder

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

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default 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. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: counting files in folder

    Quote Originally Posted by wsaryada View Post
    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. #7
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: counting files in folder

    Quote Originally Posted by DarrylBurke View Post
    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. #8
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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. #9
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default 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. #10
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: counting files in folder

    Quote Originally Posted by Gatsu View Post
    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. #11
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: counting files in folder

    Quote Originally Posted by wsaryada View Post
    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

Similar Threads

  1. Which folder do you put image files in
    By ikkyu2 in forum New To Java
    Replies: 1
    Last Post: 10-24-2011, 12:26 AM
  2. Folder & Files
    By Prajin in forum Advanced Java
    Replies: 14
    Last Post: 07-14-2010, 10:31 AM
  3. Selecting Files in a folder
    By ravjot28 in forum New To Java
    Replies: 1
    Last Post: 02-26-2010, 04:25 PM
  4. How To Place Three Files In A Folder
    By ramesh.8189 in forum AWT / Swing
    Replies: 4
    Last Post: 02-12-2009, 07:59 AM
  5. Placing Files In Folder
    By ramesh.8189 in forum New To Java
    Replies: 0
    Last Post: 02-06-2009, 12:36 PM

Posting Permissions

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