1

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*

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Logan B. Lehman
  • 4,867
  • 7
  • 32
  • 45

3 Answers3

2

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...

Daniel
  • 3,021
  • 5
  • 35
  • 50
  • Daniel, Thanks a lot for this. When I go to set the directories (which is in the DotSpatial.Controls.AppManager properties), it returns an error, "Constructor on type 'System.String' not found." So unfortunately at this time, it cannot have a value because of this. If I could fix that error, then this should not be a problem. – Logan B. Lehman Feb 08 '12 at 00:03
  • Just to be sure to gracefully handle a null 'Directories' somehow. I edited the reply to include another possible fix. – Daniel Feb 08 '12 at 01:56
0

The Directories property is null, find out why this is.

devdigital
  • 34,151
  • 9
  • 98
  • 120
0

write this with one more line of code where you compute the list of directories to make code more clear, pleaaaase !
Are you sure you are using union properly ? like AUnionB= A.Union(B) ?? Not clear in your code.

GameAlchemist
  • 18,995
  • 7
  • 36
  • 59