-2

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;
    }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Perfection
  • 721
  • 4
  • 12
  • 36
  • 3
    Where does the Exception occur ? – xlecoustillier Jan 14 '13 at 09:11
  • 1
    Attach a debugger and check for yourself. Candidates are the properties `kml` and `point` on either one of the two variables. Furthermore, the parameter itself could be null. There is nothing we can do to solve this for you, you need to do it yourself. – Daniel Hilgarth Jan 14 '13 at 09:14
  • the Point passed to the method is null or one or more of its internal properties are null (kml, point, wkb, wkt). You really should check the xLocate.Point passed in with a debugger – Steve Jan 14 '13 at 09:15
  • 1
    @Steve: wkb and wkt can be null without throwing an exception. – Daniel Hilgarth Jan 14 '13 at 09:16
  • @DanielHilgarth, yes point taken, but the problem does not change, He should use a debugger. – Steve Jan 14 '13 at 09:17
  • @X.L.Ant http://daven.nl/c/img/nullrefexception.jpg This is the exact exception that is returned. – Perfection Jan 14 '13 at 09:22
  • Also point.x/point.y have values in 100% of the test cases I have used. Other values are often null however. I have tried commenting all properties except for x and y but then it will still have returned the nullreferenceexception. x/y are doubles. – Perfection Jan 14 '13 at 09:26
  • Seems that point and kml itself have to be initialized like ken2k answered. – Perfection Jan 14 '13 at 09:45

2 Answers2

1

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
        };
    }
ken2k
  • 48,145
  • 10
  • 116
  • 176
1

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.

Simon Wang
  • 2,843
  • 1
  • 16
  • 32