0

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.

1
  • Set a breakpoint and see what object is null. Commented Apr 23, 2013 at 9:35

3 Answers 3

7

Your dummy array is not initialized. All cells are null. You need to create list in each cell, like this:

if (dummyArray[index1, index2] == null) 
  dummyArray[index1, index2] = new List<KeyValuePair<int, int>>()

Also, your code has potential for invalid index reference. If in first cycle

if (dummyData[i].Value != tempColNum)

Will evaluate to true, you will attempt to extract element from dummy array at index [-1,0]

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I just get that once the NullReference problem was solved, but this was fixed easy. Thanks.
0

It seems like the both code fragments are not really connected. In the second fragment you create a new dummyArray which only creates the list but not the elements. And you start using the elements in the remainder of the second fragment.

You should add the elements like fragement one between the creation of the list and the usage.

Comments

0

Because when you call dummyArray[index1, index2].Add(new KeyValuePair<int, int>(dummyData[i].Key, dummyData[i].Value)); the dummy array element still not initialized.

You should check null like code below:

if (dummyArray[index1, index2] == null)
{
     dummyArray[index1, index2] = new List<KeyValuePair<int, int>>();
}

Hope this help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.