-2

我正在尝试映射一个对象(它以前曾工作过!)但是,在这个新应用程序中,它似乎抛出了一个NullReferenceException我似乎不明白的问题。这是代码,有人可以解决这个问题并解释这是怎么发生的吗?

    private xRoute.Point ConvertXLocate2XRoute(xLocate.Point point)
    {
        xRoute.Point converted = new xRoute.Point();
        //KML
        converted.kml.kml = point.kml.kml;
        converted.kml.wrappedPlacemarks = point.kml.wrappedPlacemarks;

        //POINT
        converted.point.x = point.point.x;
        converted.point.y = point.point.y;

        //WKB
        converted.wkb = point.wkb;

        //WKT
        converted.wkt = point.wkt;

        return converted;
    }
4

2 回答 2

1

假设point.kmlandpoint.point不为空:

如果 的构造函数xRoute.Point没有实例化其嵌套对象属性,则必须自己进行:

converted.kml = new ...();
...
converted.point = new ...();

我还建议为此类映射器提供更简洁的代码:

    private xRoute.Point ConvertXLocate2XRoute(xLocate.Point point)
    {
        return new xRoute.Point
        {
            kml = new Kml   // Replace by the actual name of this type
            {
                kml = point.kml.kml,
                wrappedPlacemarks = point.kml.wrappedPlacemarks
            },
            point = new Point // Replace by the actual name of this type
            {
                x = point.point.x,
                y = point.point.y,
            },
            wkb = point.wkb,
            wkt = point.wkt
        };
    }
于 2013-01-14T09:13:25.710 回答
1

您的代码没有任何空引用检查,它可能是本身为空,或者point.kml为空,甚至在其他地方,您应该尝试调试在视觉上抛出异常时打开中断函数studio(CTRL+ALT+E,这里是 VS2005 的文档参考:Exceptions Dialog Box),以轻松找到它究竟是哪一行抛出异常,然后修复它。

于 2013-01-14T09:14:39.417 回答