C#中的Foreach抛出空引用异常

时间:2012-05-23 06:02:09

标签: c# list exception

我有以下代码块

string[] dirs = Directory.GetDirectories(Current);
try
{
    foreach (string dir in dirs)
    {
        string[,] dirconf = Read_Dir(dir);
        Element elem = new Element();
        elem.name = dir; // set the current name in a blank element
        if (dirconf[1, 0].Equals("container")) 
            elem.type = ElementType.Container;
        else
            elem.type = ElementType.Project; // set the current type in the element

        // this is where additionalInfo will go

        Branch.Add(elem);
     }
}
catch (Exception e)
{
    MessageBox.Show(e.Message + "\r\n" + e.Source + "\r\n" + e.InnerException + "\r\n" + e.Data + "\r\n" + e.StackTrace);
}
UpdateUI();

应该获取目录列表,读取每个目录的配置(Read_Dir函数工作正常,经过测试),然后将目录添加到Branch。分支声明如下

List<Element> Branch = new List<Element>();

使用变量监视和调试器,dirs数组获取所有正确的东西,dirconf也是如此。元素创建正常,然后,在Branch.Add我认为,它给出了一个“对象引用没有设置为对象的实例”异常,它退出foreach并直接进入UpdateUI。 UpdateUI读取分支列表,将其查找为空白,并且不执行任何操作。

0 个答案:

没有答案
相关问题