Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#中的Foreach抛出空引用异常_C#_List_Exception - Fatal编程技术网

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

C#中的Foreach抛出空引用异常,c#,list,exception,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 eleme

我有以下代码块

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函数工作正常,经过测试),然后将目录添加到分支中。分支机构声明如下:

List<Element> Branch = new List<Element>();
List分支=新列表();

使用变量watch和debugger,dirs数组获得所有正确的内容,dirconf数组也是如此。元素被创建为OK,然后,在Branch.Add,我想,它给出了一个“Object reference not set to a instance of Object”异常,在这个异常中,它退出foreach并直接转到UpdateUI。UpdateUI读取分支列表,发现它为空,什么也不做。

您应该发布堆栈跟踪。为什么要“假定”?只需调试代码。逐行检查代码,然后您将看到引发NullReferenceException的确切位置。我认为
dirconf[1,0]
是一个候选者。@Botz3000:我的水晶球说了同样的话。错误是“对象引用未设置为对象的实例”。它被抛出到Branch.Add(elem)行。我有堆栈跟踪,但我不知道如何在这里发布。