C# – ArgumentNullException was unhandled – Value cannot be null. Parameter name: first

argumentnullexceptioncnetstack-trace

I am currently using the DotSpatial library for .NET (GIS Library). I am getting an error within my AppManager class. The AppManager is a Component that manages the loading of extensions (including data providers), and helps with file serialization:

Code being flagged at foreach

public IEnumerable<string> GetDirectoriesNestedOneLevel()
{
       // Visit each directory in Directories Property (usually set by application)
    foreach (string directory in Directories.Union(new[] { "Data Extensions", "Tools" }))
    {
        string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, directory);

        if (Directory.Exists(path))
        {
            yield return path;

            // Add all of the directories in here, nested one level deep.
            var dirs = Directory.EnumerateDirectories(path, "*", SearchOption.TopDirectoryOnly);

            foreach (var dir in dirs)
            {
                yield return dir;
            }
        }
    }
}

ParamName

first

Source

System.Core

StackTrace

at System.Linq.Enumerable.Union[TSource](IEnumerable1 first, IEnumerable1 second) at
DotSpatial.Controls.AppManager.d__9.MoveNext()
in c:\dev\DotSpatial\DotSpatial.Controls\Extensions\AppManager.cs:line
581 at DotSpatial.Controls.AppManager.GetCatalog() in
c:\dev\DotSpatial\DotSpatial.Controls\Extensions\AppManager.cs:line
563 at DotSpatial.Controls.AppManager.LoadExtensions() in
c:\dev\DotSpatial\DotSpatial.Controls\Extensions\AppManager.cs:line
329 at DemoMap.MainForm..ctor() in C:\Users\Logan B.
Lehman\Documents\DemoMap\DemoMap\MainForm.cs:line 230 at
DemoMap.Program.Main() in C:\Users\Logan B.
Lehman\Documents\DemoMap\DemoMap\Program.cs:line 13 at
System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[]
args) at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args) at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx) at
System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state) at
System.Threading.ThreadHelper.ThreadStart()

Any idea on what is going on here? *It would be more than appreciated*

Best Answer

My guess would be that 'Directories' is null. It's not clear from the code snippet where that should be set, but in this case it is not being set. The error is a little cryptic because of the way Union is implemented: it is an extension method, so behind the scenes the actual call is:

IEnumerableExtensions.Union(IEnumerable first, IEnumerable second)

Depending on what Directories is, one quick fix would be, before the foreach:

if (Directories == null) { Directories = new List<string>().ToArray(); }

Another possibility would be something like:

var allDirs = new List<string>();
if (Directories != null) { allDirs.AddRange(Directories);}
allDirs.AddRange(new[]{ "Data Extensions", "Tools" });
foreach(string directory in allDirs)

But a better fix would be to go to the code that sets directories and make sure it's always setting a value...

Related Topic