Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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# 将二维字符串数组转换为二维整数数组_C#_Arrays_String_Int_Nullreferenceexception - Fatal编程技术网

C# 将二维字符串数组转换为二维整数数组

C# 将二维字符串数组转换为二维整数数组,c#,arrays,string,int,nullreferenceexception,C#,Arrays,String,Int,Nullreferenceexception,我正在尝试将二维字符串数组转换为二维int数组: int[][] inner = new int[4][]; string[][] arr = new string[4][] { new string[] {"11"}, new string[] {"12"}, new string[] {"21"}, new string[] {"22"} }; for (int i = 0; i < arr.Length; i++) { string nam

我正在尝试将二维字符串数组转换为二维int数组:

int[][] inner = new int[4][];

string[][] arr = new string[4][]
{
    new string[] {"11"},
    new string[] {"12"},
    new string[] {"21"},
    new string[] {"22"}
};

for (int i = 0; i < arr.Length; i++)
{
    string name = string.Join(".", arr[i]);
    for (int j = 0; j < name.Length; j++)
    {
        inner[i][j] = Convert.ToInt32(name.Substring(j,1));
    }
}

将“内部”变量的声明更改为

int[,] inner = new int[4,2];

几乎所有
NullReferenceException
的情况都是相同的。有关一些提示,请参见“”。
内部[0]
为空。这将导致您看到的NRE。在该行上放置一个断点,然后查看空值,简单。您创建了一个二维数组,但只填充了一个维度。使用j变量迭代的数组都是空的,因此您最终会调用null.Substring,从而导致异常
int[,] inner = new int[4,2];