首页 > 解决方案 > NullException:值不能未定义。参数名称:来源

问题描述

        //get entries
        string[,] entries = Search(ReplaceBlankWithAny(
          new string[] { name, code, supplier, lot, grams, date, shelf, inStock, costKG }), 
          data);

        int length = entries.GetLength(0);
        int x = 0;

        if (entries.GetLength(1) != 0)
        {
            x = 0;
            //populate table
            //for (uint x = 0; x < entries.GetLength(0); x++) not working
            while (x < length)
            {
                string[] onlyInterestingParts = entries
                  .TakeHorizontalSlice((uint)x)
                  .Take(4)
                  .ToArray();

                Main.instance.dataGridViewMatPrime.Rows.Add(onlyInterestingParts);

                x++;
            }
        }

        return entries;

我尝试使用 for (在代码中注释)然后一段时间,但它们都返回标题中的错误。我将变量 x 移到 if 之外,但它仍然不起作用。我确定ENTRIES IS NOT NULL,既因为 if (entries.GetLength(1) != 0) 为真,又因为我在调试器中检查了它。任何回复都非常感谢!

标签: c#winformsfor-loopwhile-loopnull

解决方案


问题在 while (x < length) 上显示,但它在

string[] onlyInterestingParts = entries
                  .TakeHorizontalSlice((uint)x)
                  .Take(4)
                  .ToArray();

TakeHorizo​​ntalSlice((uint)x) 返回 null


推荐阅读