Hi,
Below is the declaration at class level.

int[] PossibleValues = new int[6];
int[][] JaggedArray = new int[50][];
int JaggedVar = 0;

Then in a function, I am assigning PossibleValues array to Jaggedarray like this.

[PossibleNumbers is a function that returns an array of possible numbers for a cell in a grid.]

PossibleValues = PossibleNumbers();
JaggedArray[jaggedVar] = PossibleValues;
jaggedVar++;

The number of elements in PossibleValues in each iteration may vary,but it will never exceed 6.I dont want Jaggedarray to store zeros of PossibleValues so I changed the code to:

PossibleValues = PossibleNumbers();
for (int i = 0; i < PossibleValues.Length; i++)
{
   if (PossibleValues[i] != 0)
   {
      JaggedArray[jaggedVar][i] = new PossibleValues[i];
   }
}
jaggedVar++;

But this gives null reference exception at
JaggedArray[jaggedVar][i] = new PossibleValues[i];

I searched it over the internet and came to know that JaggedArray remains null hence values cannot be assigned this way.How to resolve this?
Any help would be appreciated.
Thanks in advance!

I wouldn't recommend using arrays at all in C#. They don't play nice with a lot of other objects.

A list of lists of ints would probably work best for you here:

var JaggedList = new List<List<int>>();
for (int i = 0; i < 50; i++)
    JaggedList.Add(new List<int>(PossibleNumbers()));

You can access the items exactly the same as you could with the jagged array:

int myNumber = JaggedList[5][12];

Is it a requirement for you to use a Jagged Array?
If so, check out this post:
Otherwise, @skatamatic is correct: there are many easier(and more powerful) things you can do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.