0

Below is my code,

List<float?> LValues = new List<float?>();
List<float?> IValues = new List<float?>();
List<float?> BValues = new List<float?>();
List<HMData>[] data = new List<HMData>[4];
List<HMData>[] Data = new List<HMData>[7];
float? Value_LfromList = 0;
float? Value_IfromList = 0;
float? Value_BfromList = 0;
int indexer=0;

foreach (var item in Read_xml_for_childobjects_id.Root.Descendants("object"))
{
data[indexer] = new List<HMData>();  // Error occuring on this line
for (int k = 0; k < 7; k++)
  {
    Value_LfromList = LValues.ElementAt(k);
    Value_IfromList = IValues.ElementAt(k);
    Value_BfromList = BValues.ElementAt(k);
    Data[k].Add(new HMData { x = Value_LfromList, y = Value_IfromList, z = Value_BfromList });
  } 
  indexer++;
 }

As soon as I intend to add the element at Data list in following line,

Data[k].Add(new HMData { x = Value_LfromList, y = Value_IfromList, z = Value_BfromList });

I get an error as Object reference not set to instant of object,

I want output be as shown in following question link, Result required as shown in this question,

I have tried by lots of ways but could not make it,will really appreciate help if provided,Thanks.

Community
  • 1
  • 1
Reshma
  • 864
  • 5
  • 20
  • 38
  • 1
    First problem - never have two variables `data` and `Data`. That's a killer for readability. What's the *useful* difference between the two meant to be between those variables? – Jon Skeet Jan 06 '14 at 06:44
  • Actually..I was just trying trying to get it work done,there was initially only one data list but later on I created one more,my requirement is weird and it seems impossible, but then in coding everything is possible just might I am wrong somewhere in logic..can you please check the link given in above question for another question and just tell me what I am thinking to achieve his possible... – Reshma Jan 06 '14 at 06:48
  • Well having code which is difficult to read isn't going to make it any easier to get the work done. Do *you* even know the difference between what the two variables are meant to represent? – Jon Skeet Jan 06 '14 at 06:49
  • ok...I totally agree with you and apologize for the mess..can you please follow this link [http://stackoverflow.com/questions/20905207/adding-values-to-list] and guide me...Please.. – Reshma Jan 06 '14 at 06:53
  • You've already got an answer on that question... and the update to it is very unclear. – Jon Skeet Jan 06 '14 at 06:54
  • I am not getting desired result..c..when I create List[] data = new List[4];.. – Reshma Jan 06 '14 at 07:01
  • then I get data list containing 4 count where each count should againg containe 7 data values, my other list like LValues,IValues and BValues in all contains 28 elements and I want first I want getting one common data list containing values as data={x=Lvalues[0],y=IValues[0],z=BValues[0]......x=Lvalues[28],y=IValues[28],z=BValues[28]} ,here 28 values are of 4 child objects and I need to pass them to highcharts to plot bubble chart bt this all values comes under one series, so problem is I need to plot for different data list for 4 different child objects so I can take them as different series – Reshma Jan 06 '14 at 07:01
  • I hope my question is clear now,if not please let me know...just want to plot 4 data list and each data list containing 7 data values required the values are taken from there respective objects – Reshma Jan 06 '14 at 07:03

2 Answers2

6
  1. Your code is a nightmare. You should really think about refactoring...

  2. You have to initialize the lists within Data array.

    List<HMData>[] Data = new List<HMData>[7];
    for(int i = 0; i < 7; i++)
        Data[i] = new List<HMData>();
    
  3. There are tons of other problems and questions that should be asked (like what's the difference between data and Data?, why are these array sized explicitly?). Without that knowledge every advice can be not enough to solve your real problem.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • please follow this link [http://stackoverflow.com/questions/20905207/adding-values-to-list] and please tell me, is it possible to achieve the desired output...I have ended in nightmare achieving this output and I am extremely sorry for the confusion.. – Reshma Jan 06 '14 at 06:51
-2

you just need to declare the list as

List<HMData> Data = new List<HMData>();

and add new element to the list by

 Data.Add(new HMData { x = Value_LfromList, y = Value_IfromList, z = Value_BfromList });
Cris
  • 12,799
  • 5
  • 35
  • 50
  • 1
    That won't even compile - you're declaring an array variable, and then trying to assign it a plain `List` reference. – Jon Skeet Jan 06 '14 at 06:44
  • Now you're assuming that the OP really doesn't want an array of lists... that's not clear to me based on the question. – Jon Skeet Jan 06 '14 at 07:50