3 Replies Latest reply: Jun 26, 2007 6:48 AM by 807605 RSS

    exception while reading a directory

    807605
      hi,
      i got a problem , i have a list of 2000 files in a directory and i haev to read it and process it,
      am using file class to read as a directory , but the following code shows null pointer exception,
      can anybody help me,
      File database = new File("c:/gunasekp/Pdbfile/grepdatabase");
              String datafiles[] = database.list();
              
            for (int i = 0; i < datafiles.length; i++){
            System.out.println("processing file: " + database.getAbsolutePath() +  "/" + datafiles[0]);
       
              }
        • 1. Re: exception while reading a directory
          807605
          if you get a null pointer exception here your file "database" doesn't exist or isn't a directory. Check for existance and directory before you call the list method like:
          File database = new File("c:/gunasekp/Pdbfile/grepdatabase");
                    
          if (database.exists() && database.isDirectory())
          {
            String datafiles[] = database.list();
               
            for (int i = 0; i < datafiles.length; i++)
            {
              System.out.println("processing file: " + database.getAbsolutePath() +
                                  "/" + datafiles[0]);
               
            }
          }
          else
          {
            System.out.println("Database exists: "+database.exists()+"\nDatabase is Directory: "+database.isDirectory());
          }
          • 2. Re: exception while reading a directory
            807605
            Check the return value of
             database.list(); 
            • 3. Re: exception while reading a directory
              807605
              This path not exists and the database.list() is returning null .
              so, datafiles.length is throwing NullPonterException .

              Use a valid path and get certified that the file.exists() returns true

              Cya.