将包含公式的循环中的每个答案添加到C#中的列表

Age*_*een -2 c# loops list nullreferenceexception

我有一个计算数字的公式,然后我需要把每一项的倍数,并将其存储到一个有序列表,多达数,因为它需要得到300我相信一个for循环是不是最好的这样做的方法,但这就是我得到的.

public List<double> axialLengthFt(double length)
    {
       fundamental = (1130 / 2) / length;

       for (int i = 1; i < 15; i++)
       {    
           double d = fundamental * i;
           if (d <= 300)
              modes.Add(d); //NullReferenceException here??
           else
               break;              
       }
        return modes;
    }
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我得到NullReferenceException,请帮忙!

Joh*_*aft 5

你永远不会声明变量模式.

List<double> modes = new List<double>();
Run Code Online (Sandbox Code Playgroud)