C# 将项添加到列表时引发异常

C# 将项添加到列表时引发异常,c#,C#,我相当困惑。我在包含GeoRoot.features.Add(Feat)的行中得到一个对象引用错误,在我看来它就像一个列表。我做错了什么 public double getDistance(GeoCoordinate p1, GeoCoordinate p2) { double d = p1.Latitude * 0.017453292519943295; double num3 = p1.Longitude * 0.017453292519943295; double

我相当困惑。我在包含
GeoRoot.features.Add(Feat)的行中得到一个对象引用错误,在我看来它就像一个列表。我做错了什么

public double getDistance(GeoCoordinate p1, GeoCoordinate p2)
{
    double d = p1.Latitude * 0.017453292519943295;
    double num3 = p1.Longitude * 0.017453292519943295;
    double num4 = p2.Latitude * 0.017453292519943295;
    double num5 = p2.Longitude * 0.017453292519943295;
    double num6 = num5 - num3;
    double num7 = num4 - d;
    double num8 = Math.Pow(Math.Sin(num7 / 2.0), 2.0) + ((Math.Cos(d) * Math.Cos(num4)) * Math.Pow(Math.Sin(num6 / 2.0), 2.0));
    double num9 = 2.0 * Math.Atan2(Math.Sqrt(num8), Math.Sqrt(1.0 - num8));
    return (6376500.0 * num9);
}

public GeoRootObject GetRndNearybyLocationList(double lat, double lon, int meters)
{
    GeoRootObject GeoRoot=new GeoRootObject();


    LocationObject thisRndLocation = new LocationObject();
    List<LocationObject> locationsList = new List<LocationObject>();

    //List<GeoJSON.Net.Geometry.GeographicPosition> Positions = new List<GeoJSON.Net.Geometry.GeographicPosition>();

    Random rnd = new Random();
    int dice = rnd.Next(1, 7);


    for (int i = 1; i <= dice; i++)
    {
        thisRndLocation = getLocation(lat, lon, meters);
        GeoRoot.type = "FeatureCollection";
        Feature Feat = new Feature();
        Feat.type = "Point";
        List<double> coOrds = new List<double>();
        coOrds.Add(thisRndLocation.lon);
        coOrds.Add(thisRndLocation.lat);
        GeoRoot.features.Add(Feat);
        Geometry Geometry = new Geometry();
        Geometry.coordinates = (coOrds);
        Geometry.type = ("Point");
        Feat.geometry = Geometry;
        Feat.id = i;
        GeoRoot.features.Add(Feat);

    }
    return GeoRoot;
}
public double getDistance(地理坐标p1、地理坐标p2)
{
双d=p1.纬度*0.017453292519943295;
双num3=p1.经度*0.017453292519943295;
双num4=p2.纬度*0.017453292519943295;
双num5=p2.经度*0.017453292519943295;
双num6=num5-num3;
双num7=num4-d;
double num8=Math.Pow(Math.Sin(num7/2.0),2.0)+(Math.Cos(d)*Math.Cos(num4))*Math.Pow(Math.Sin(num6/2.0),2.0));
double num9=2.0*Math.Atan2(Math.Sqrt(num8),Math.Sqrt(1.0-num8));
返回(6376500.0*num9);
}
公共地理对象GetRndNearybyLocationList(双lat、双lon、int米)
{
GeoRootObject GeoRoot=新的GeoRootObject();
LocationObject thisRndLocation=新的LocationObject();
列表位置列表=新列表();
//列表位置=新列表();
随机rnd=新随机();
int dice=rnd.Next(1,7);

对于(int i=1;i您似乎遇到的主要问题是,您试图在空列表上调用
.Add()
方法。在出现问题的那一行之前,尝试添加
GeoRoot.features=new list());

您的“功能”列表似乎尚未初始化。您还混合了命名约定。公共方法和属性应为PascalCase。您的意思是这个GeoRootObject GeoRoot=new GeoRootObject();它是在方法中忘记粘贴的,很好的一点是关于我使用工具生成类的情况,我一定会整理
public class Geometry
{
    public string type { get; set; }
    public List<double> coordinates { get; set; }
}

public class Properties
{
    public string popupContent { get; set; }
}

public class Feature
{
    public Geometry geometry { get; set; }
    public string type { get; set; }
    public Properties properties { get; set; }
    public int id { get; set; }
}

public class GeoRootObject
{
    public string type { get; set; }
    public List<Feature> features { get; set; }
}