1

I have the following method, which is meant to add file names to a List<string> so that the desired order of the files is stored in the Program class:

    private static void ConvertPdfs()
    {
        // Get the word files FileInfo
        FileInfo[] wordFileInfo = DataGrabber.GetHrWordFileInfo(sourceFolder, spSite, policyListName, sortBy);

        foreach (FileInfo file in wordFileInfo)
        {
            // Create the ordered list - this adds each document to the list in the correct oder (sort in CAML query)
            orderedListOfFileNames.Add(file.Name);                
        }           
        Converter convert = new Converter();
        convert.ToPdf(wordFileInfo, targetPdf);
    }

Where ordereListOfFileNames is a field in that same class:

 private static List<string> orderedListOfFileNames; // Stil static ...

When the method loops around wordFileInfo, I am seeing this exception:

An unhandled exception of type 'System.NullReferenceException' Occurred in PdfConverter.exe
Additional information: Object reference not set to an instance of an object.

However, I can see that wordFileInfo contains 22 items, all of which have a name.

Is the issue here the fact that orderedListOfFileNames has not been initialised properly?

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Bassie
  • 9,529
  • 8
  • 68
  • 159

2 Answers2

3

You need to instantiate the orderedListOfFileNames before adding value to the list using a new operotor as like the following:

private static List<string> orderedListOfFileNames= new List<string>(); 
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
2

instantiate is not necessary when you are doing the following so thats not the issue.

FileInfo[] wordFileInfo = DataGrabber.GetHrWordFileInfo(sourceFolder, spSite, policyListName, sortBy);

But still when you do the above code wordFileInfo becomes null, so DataGrabber.GetHrWordFileInfo(sourceFolder, spSite, policyListName, sortB) something happends here.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Hanna
  • 21
  • 1