C# 将二维字符串数组转换为二维整数数组
我正在尝试将二维字符串数组转换为二维int数组: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[][] 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];