0

I have a list of Series for signals that are being imported from a file. I want to have all X values of each datapoint of every Series in one seperate list and be able to access them via a loop. I was thinking of creating an array of Lists.

Here is what I have tried so far.

List<double>[] datapointsX = new List<double>[Signals.Count];
int i=0;
foreach(var signal in Signals){
 datapointsX[i] = new List<double>();
 for(int j=0; j<signal.Points.Count; j++){
datapointsX[i].Add(signal.Points[j].XValue);
}
Console.Writeline(datapointsX[0][1]);
i++;
}

But I get an exception that the object is not set to an instance or an object. The error is at the Console.Writeline function.

nickolas
  • 121
  • 7
  • any specific reason for not using simply a `List>` ? – Franck Jul 30 '21 at 12:11
  • In the first line you create an array to hold multiple lists. But you didn't create an instance of list (there is no `new List()` in your code) before trying to call `Add` method. – Sinatr Jul 30 '21 at 12:11
  • 2
    Between the `foreach` and the `for` add: `datapointsX[i] = new List();` Array contents are initialised to null, so the elements at `datapointsX[i]` will all be null unless you explicitly initialise the elements. – Matthew Watson Jul 30 '21 at 12:12
  • 2
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Code Stranger Jul 30 '21 at 12:36
  • I edit it to be more clear – nickolas Jul 30 '21 at 15:20

0 Answers0