Question

I'm trying to map an object (and it has worked before!) however, in this new application it seems to throw a NullReferenceException that I don't seem to understand. Here is the code, can someone solve this issue and explain how this can happen?

    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;
    }
Was it helpful?

Solution

Assuming point.kml and point.point are not null:

If the constructor of xRoute.Point does not instantiate its nested object properties, you must do it by yourself:

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

I would also suggest a more concise code for such mappers:

    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
        };
    }

OTHER TIPS

Your code do not have any null reference check, and it could be the point itself is null, or point.kml is null, or even somewhere else, you should try with debugging with turn the break when an exception is thrown function on in visual studio (CTRL+ALT+E, here is the doc reference for VS2005: Exceptions Dialog Box) to find which line exactly it is throwing exception easily and then fix it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top