-2

I am trying to access all the files inside sub folders which are present inside a master folder, along with their last access times.

I am able to get all the files, the problem arises when I'm trying to get the last access times.

I receive the 'Object not set to an instance of an object' exception.

I am not able to see the anomaly in my code.

Below is the code:

public List<String> GetDirs(string Dir)
        {
            List<String> files = new List<String>();
            try
            {
                foreach (string f in Directory.GetFiles(Dir))
                {
                    files.Add(f);
                }
                foreach (string d in Directory.GetDirectories(Dir))
                {
                    files.AddRange(GetDirs(d));
                }
                int num=files.Count;
                FileInfo[] fileinfo=null;
                for (int i = 0; i < num; i++)
                {
                    fileinfo[i] = new FileInfo(files[i]);//Exception at this line of code

                }
                foreach(FileInfo xx in fileinfo)
                {
                    Response.Write(xx.LastWriteTime);
                }
              }
            catch (System.Exception excpt)
            {
                Response.Write(excpt.Message);
            }

            return files;
        }

The code is called as:

    string fullPath = @"D:\Main\Folder1";
    List<string> lst=GetDirs(fullPath);

The code is not complete at this time.

There are 3 text files inside Folder1, such as:

D:\Main\Folder1\a.txt

D:\Main\Folder1\bin\a1.txt

D:\Main\Folder1\SubFolder1\sa1.txt

I am trying to get the last access times for the above files too, but the exception doesn't permit to go further.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Anurag
  • 552
  • 9
  • 31
  • Sorry, but you have the debugger, probably the entire Visual Studio support, and you don't know the exactly line of the exception? – gustavodidomenico Jun 12 '14 at 17:44
  • Well which line is throwing the exception, and what have you done to diagnose it? – Jon Skeet Jun 12 '14 at 17:45
  • 1
    By inspection, `fileinfo[i]` is going to fail as `fileinfo` is null... How did you expect that to work? – Jon Skeet Jun 12 '14 at 17:45
  • @JonSkeet fileinfo[0]=new FileInfo(D:\Main\Folder1\a.txt)..I think on the first iteration, the above line of code should arrive.. – Anurag Jun 12 '14 at 17:47
  • @gustavodidomenico I have debugged and have pointed out the line of code which throws the exception. Have a look again to the question. – Anurag Jun 12 '14 at 17:49
  • You must agree with me, that you could make things easier... – gustavodidomenico Jun 12 '14 at 17:50
  • @gustavodidomenico I totally second your point. Next time onwards I will make the codes and comments pretty clearer. – Anurag Jun 12 '14 at 17:51

3 Answers3

2

It's pretty obvious here:

FileInfo[] fileinfo=null;   // <-- fileinfo is null
for (int i = 0; i < num; i++)
{
    fileinfo[i] = new FileInfo(files[i]);//Exception at this line of code
//   ^--- fileinfo is still null!
}

you are trying to use the indexer on a null reference to set a value. I think you want:

FileInfo[] fileinfo= new FileInfo[num];
for (int i = 0; i < num; i++)
{
    fileinfo[i] = new FileInfo(files[i]);//Exception at this line of code
}

or just

FileInfo[] fileinfo = files.Select(f => new FileInfo(f)).ToArray();

EDIT

After reading your comments, it seems that you think that FileInfo[] fileinfo=null; will initialize an array of null values (which is true in some languages, but not C#). In fact, it creates an array variable that is itself a null value. To initialize an array, you use the syntax FileInfo[] fileinfo=new FileInfo[size]; which will create an array of null values (more precisely, of the default value for FileInfo, which is null since it's a reference type).

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

You are not initializing FileInfo[] fileinfo.

Fix: FileInfo[] fileInfo = new FileInfo[num]

Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103
0

Your code seems...a little complex...for what it does. Wouldn't it be a lot easier to let the framework do the work for you and say something like this:

public static IEnumerable<FileSystemInfo> ContentsOfDirectoryTreeRootedAt( string rootDirectory )
{
  DirectoryInfo root = new DirectoryInfo( rootDirectory ) ;
  if ( !root.Exists ) throw new DirectoryNotFoundException(rootDirectory) ;

  foreach( FileSystemInfo entry in root.EnumerateFileSystemInfos(null,SearchOption.AllDirectories) )
  {
    yield return entry ;
  }

}

Then you can invoke it along these lines:

foreach( FileInfo fi in ContentsOfDirectoryTreeRootedAt( @"C:\foo\bar" ).Where( x => x is FileInfo ).Cast<FileInfo>() )
{
  Console.Write( "FileName: {0}" , fi.FullName ) ;
  Console.Write( "\t" );
  Console.Write( "Last Updated: {1}" , fi.LastWriteTime ) ;
  Console.Write( "\t" );
  Console.Write( "Size: {0:0.0}kb" , (decimal)fi.Length / 1024m ) ;
  Console.WriteLine() ;
}
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135