0

I'm trying to convert a two dimensional string array to a two dimensional int array:

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));
    }
}

But I'm getting the following exception:

Object reference not set to an instance of an object

at:

inner[i][j] = Convert.ToInt32(name.Substring(j,1));
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Afnan
  • 35
  • 1
  • 1
  • 5
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 23 '13 at 22:58
  • `inner[0]` is null. That will cause the NRE you're seeing. – carlosfigueira May 23 '13 at 22:58
  • Put a break point on that line and see what is null, simple. – alanh May 23 '13 at 23:03
  • 3
    you have created a two dimensional array, but only populated one dimension. The arrays iterated over with the j variable are all empty so you end up calling null.Substring which causes the exception – NeddySpaghetti May 23 '13 at 23:13

1 Answers1

0

Change the declaration of your "inner" variable to

int[,] inner = new int[4,2];
Markis
  • 1,703
  • 15
  • 14