I want to make some dummy data to use in my asp.net mvc 3 view. The following code is part of the controller that should pass the data to the view.
List<KeyValuePair<int, int>> dummyData = new List<KeyValuePair<int, int>>();
dummyData.Add(new KeyValuePair<int, int>(1,1));
dummyData.Add(new KeyValuePair<int, int>(1,2));
dummyData.Add(new KeyValuePair<int, int>(2,1));
dummyData.Add(new KeyValuePair<int, int>(3,1));
dummyData.Add(new KeyValuePair<int, int>(4,1));
dummyData.Add(new KeyValuePair<int, int>(4,2));
dummyData.Add(new KeyValuePair<int, int>(4,3));
dummyData.Add(new KeyValuePair<int, int>(4,4));
As the name says this is my dummy data. The idea behind this is that the first number represents a RowNumber form a table, and the second number represents a ColumnNumber. I want to somehow combine the records which are related to the same Row but has different ColumnNumbers. For this I chose to use two dimensional array :
int dummyCount = dummyData.Count;
List<KeyValuePair<int, int>>[,] dummyArray = new List<KeyValuePair<int, int>>[dummyCount,dummyCount];
int index1 = -1;
int index2 = 0;
for (int i = 0; i < dummyCount; i++)
{
int tempColNum = 1;
if (dummyData[i].Value != tempColNum)
{
dummyArray[index1, index2].Add(dummyData[i]);
index2++;
}
else
{
index1++;
index2 = 0;
dummyArray[index1, index2].Add(new KeyValuePair<int, int>(dummyData[i].Key, dummyData[i].Value));
}
}
But when I get here : dummyArray[index1, index2].Add(new KeyValuePair<int, int>(dummyData[i].Key, dummyData[i].Value)); I get the error from the title : Object reference not set to an instance of an object.. Originally I tried only dummyArray[index1, index2].Add(dummyData[i]); but got the same error.