-3

I want to add a number to a List<double>[] distances = new List<double>[2];

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 5; j++)
    {
         double distance = MyFunktionthatreturnsANumber();
         distances[i].Add(distance);
    }
}

But everytime I try to run this code i get this error at the first passage if the loop:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

wrgsaf
  • 1
  • 1

1 Answers1

2

You're declaring an array of List<double> objects, but you aren't initializing the elements of that array to an object?

List<double>[] distances = new List<double>[2];
distances[0] = new List<double>();
distances[1] = new List<double>();

# continue
nablag
  • 176
  • 1
  • 7