Question

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.

Was it helpful?

Solution

  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.

OTHER TIPS

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 });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top