0

This is my code

var dictionary = new Dictionary<string, List<Int64[][]>>();
for (int i = 0; i < compainNames.Count(); i++)
{
    List<Int64[][]> data = new List<Int64[][]>();
    var dataForOneCampaint = DTgraph.Select("Campaign = '" + compainNames[i].CampaignName.ToString() +"'").ToList();
    for (int j = 0; j < dataForOneCampaint.Count(); j++)
    {
        Int64[][] array = new Int64[1][];
        array[0][0] = (Int64)dataForOneCampaint[j].Field<Decimal>("Inb.ServiceLevel");
        DateTime d = DateTime.Parse(dataForOneCampaint[j].Field<string>("Date").ToString());
        array[0][1] = d.Ticks;
        data.Add(array);
    }
    dictionary.Add(compainNames[i].ToString(), data);
}

where DTgraph is a datatable.

I got this exception {"Object reference not set to an instance of an object."}

on this line : array[0][0] = (Int64)dataForOneCampaint[j].Field<Decimal>("Inb.ServiceLevel");

I can see that the dataForOneCampaint has 16 values.

and I have used the same field Inb.ServiceLevel in another function exactly like I did in this code and It works fine.

where am I making wrong please?

rene
  • 41,474
  • 78
  • 114
  • 152
user2208349
  • 7,429
  • 5
  • 26
  • 35

1 Answers1

4

Jagged array initialization have to be performed twice: once for outer dimension and once for each inner array. It's described more precisely on MSDN: Jagged Arrays (C# Programming Guide).

In your case you should do following:

Int64[][] array = new Int64[1][];
array[0] = new Int64[2];
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • whey did you user `2` ? I need this kind `[[1,4][5,1]...[44,1]]` so I guess it should be just one , right? – user2208349 Apr 20 '14 at 08:20
  • I used `2` because later in your code you're assigning values to `array[0][0]` and `array[0][1]`, which means you need 2 items in inner array. – MarcinJuraszek Apr 20 '14 at 08:21
  • it works. i will accept the answer once the systems allows. +1 – user2208349 Apr 20 '14 at 08:28
  • @MarcinJuraszek I've update the [canonical answer](http://stackoverflow.com/a/4660186/578411) with your jagged array sample. Please check if I've done that correctly. – rene Apr 20 '14 at 08:44
  • @rene looks fine. You could probably use `int[][]` or `long[][]` instead of `Int64` to make it more natural, but that's just a detail (I used `Int64` because OP uses it). – MarcinJuraszek Apr 20 '14 at 09:08
  • @MarcinJuraszek Ok, I'll edit that in... – rene Apr 20 '14 at 09:11