成功创建后从数据库中拉出以创建对象时出错

时间:2018-08-04 17:57:21

标签: c# xamarin sqlite static nullreferenceexception

让我首先为这篇文章的复杂性道歉。希望我缺少一些简单的东西,但是为了找出答案,我必须做一个冗长的解释。

我正在构建一个人员跟踪应用程序,该应用程序允许用户在地图上绘制多边形。多边形用作区域。如果设备位置在区域内,则会将它们设置为一个状态,例如“进入”,然后在离开时将其设置为另一个状态,例如“离开”。我在用几个小部件。 SQLite,TK.CustomMap,PopupPlugin,以在共享项目中完成尽可能多的任务。 SQLite数据模型基于该解决方案的远程数据库的结构,该解决方案还具有桌面应用程序,Web应用程序和许多其他接口,因此必须维护该结构。

此操作涉及的表是Zones,ZonePoints和StatusClass。保存区域后,TK.Polygon的点将保存到点表中。分配给区域的进出状态的状态和区域名称一样分配给区域。

该过程以这种方式工作-首先,用户单击按钮以添加区域。这将创建一个新区域,将其保存到数据库并从表中获取其ID。目前,虽然在API准备就绪时,ZoneSys(远程数据库中的主键)也已设置为ID,但将在远程数据库中自动递增。这是该命令及其引用的对象。考虑到它们是第一次使用的,所以我没有包括数据库方法定义,但是如果有人认为它们会帮助解决这个难题,请告诉我-

    public static List<Position> ActiveZonePositions = new List<Position>();
    public static int ActiveZoneID;
    public static Zone ActiveZone;
    public static int PointCount;
    public static bool NewZoneOpen = false;

    public Command<EventArgs> OpenNewZone
    {
        get
        {
            return new Command<EventArgs>(async e =>
            {
                NewZoneOpen = true;
                PointCount = 0;
                ActiveZonePositions.Clear();
                Zone ZoneToAdd = new Zone
                {
                    Contactsys = MobileUser.ContactSys,
                    ZoneTypeSys = 1,
                    OrganizationSys = MobileUser.OrganizationSys,
                    ZoneName = null
                };

                ActiveZoneID = await AddZoneToDBAsync(ZoneToAdd);
                ZoneToAdd.ID = ActiveZoneID;
                ZoneToAdd.ZoneSys = ActiveZoneID;
                await AddZoneToDBAsync(ZoneToAdd);
                ActiveZone = await App.Database.GetZoneAsync(ActiveZoneID);
            });
        }
    }

当用户单击地图中的点时,将使用这些位置绘制多边形,这些位置也用于创建添加到静态列表的点。这是MapClicked_Command-

public Command<Position> MapClickedCommand
{
    get
    {
        return new Command<Position>(async position =>
        {
            if (NewZoneOpen)
            {
                bool isPointInPolygon = IsPointInAnyPolygon(position);

                if (isPointInPolygon)
                {
                    var action = await Application.Current.MainPage.DisplayActionSheet(
                                                        "Region Collides with Another Region",
                                                        "Cancel",
                                                        null,
                                                        "Try Again",
                                                        "Close Zone Editor");
                    if (action == "Close Zone Editor")
                    {
                        await RemoveZoneAsync(ActiveZoneID);
                    }
                    if (action == "Try Again")
                    {
                        return;
                    }
                }

                else if (!isPointInPolygon)
                {
                    ActiveZonePositions.Add(position);
                }

                if (ActiveZonePositions.Count == 2)
                {

                    ZonePolyLine.LineCoordinates = ActiveZonePositions;
                    _lines.Remove(ZonePolyLine);
                    _lines.Add(ZonePolyLine);
                }
                else if (ActiveZonePositions.Count == 3)
                {
                    ActiveZonePositions.Add(position);
                    _lines.Remove(ZonePolyLine);
                    TKPolygon poly = new TKPolygon
                    {
                        StrokeColor = System.Drawing.Color.CornflowerBlue,
                        StrokeWidth = 2f,
                        Color = System.Drawing.Color.CornflowerBlue
                    };
                    foreach (Position pos in ActiveZonePositions)
                    {
                        poly.Coordinates.Add(pos);
                    }
                    _polygons.Add(poly);
                    _currentPolygon.Clear();
                    _currentPolygon.Add(poly);
                    currentPolygonRendering = true;
                }
                else if (ActiveZonePositions.Count > 3)
                {
                    ActiveZonePositions.Add(position);
                    TKPolygon poly = new TKPolygon
                    {
                        StrokeColor = System.Drawing.Color.CornflowerBlue,
                        StrokeWidth = 2f,
                        Color = System.Drawing.Color.CornflowerBlue
                    };
                    foreach (Position pos in ActiveZonePositions)
                    {
                        poly.Coordinates.Add(pos);
                    }
                    _polygons.Remove(_polygons.Last());
                    _polygons.Add(poly);
                    _currentPolygon.Clear();
                    _currentPolygon.Add(poly);
                }

                var pin = new TKCustomMapPin
                {
                    Position = new TK.CustomMap.Position(position.Latitude, position.Longitude),
                    Title = string.Format("Pin {0}, {1}", position.Latitude, position.Longitude),
                    IsVisible = true,
                    IsDraggable = true,
                    ShowCallout = true
                };
                _pins.Add(pin);
                await CreatePointAsync(position);
                PointCount++;
            }


            else if (EditZoneOpen)
            {
                ActiveZonePositions.Add(position);

                var poly = _polygons[0];
                poly.Coordinates.Clear();
                foreach (Position pos in ActiveZonePositions)
                {
                    poly.Coordinates.Add(pos);
                }
                _polygons.Remove(_polygons.Last());
                _polygons.Add(poly);
                _currentPolygon.Clear();
                _currentPolygon.Add(poly);

                var pin = new TKCustomMapPin
                {
                    Position = new TK.CustomMap.Position(position.Latitude, position.Longitude),
                    Title = string.Format("Pin {0}, {1}", position.Latitude, position.Longitude),
                    IsVisible = true,
                    IsDraggable = true,
                    ShowCallout = true
                };
                _pins.Add(pin);
                await CreatePointAsync(position);
                PointCount++;
            }

        });
    }
}

这是CreatePointAsyncMethod-

public async Task CreatePointAsync(TK.CustomMap.Position position)
{
    var zone = await RetrieveZoneAsync(ActiveZoneID);
    Model.Point PointToAdd = new Model.Point
    {
        ZoneSys = zone.ZoneSys,
        PointName = "",
        Latitude = position.Latitude,
        Longitude = position.Longitude,
        PointOrder = PointCount + 1
    };
        ActiveZonePoints.Add(PointToAdd);
}

这里是IsPointInAnyPolygon方法,该方法对照多边形列表进行检查以确保单击的点不在它们的任何内部以及其支持方法。

private bool IsPointInAnyPolygon(Position position)
{
    bool inBounds = false;
    for (var i = 0; i < ZonePolygons.Count(); i++)
        foreach (ZonePolygon zpoly in ZonePolygons)
        {
            TKPolygon tkpoly = zpoly.Zpolygon;
            inBounds = IsPointInPolygon(position, tkpoly.Coordinates);
            if (inBounds)
            {
                ActiveZoneID = zpoly.ID;
                return inBounds;
            }
        }
    return inBounds;
}

private bool IsPointInPolygon(TK.CustomMap.Position position, List<Position> coords)
{
    int intersectCount = 0;
    for (int j = 0; j < coords.Count() - 1; j++)
    { 
        if (j+1 >= coords.Count())
        {
            if (rayCastIntersect(position, coords[j], coords[0]))
            {
                intersectCount++;
            }
        } else if (rayCastIntersect(position, coords[j], coords[j + 1]))
        {
            intersectCount++;
        }
    }

    return ((intersectCount % 2) == 1); // odd = inside, even = outside;
}

private bool rayCastIntersect(TK.CustomMap.Position position, TK.CustomMap.Position vertA, TK.CustomMap.Position vertB)
{

    double aY = vertA.Latitude;
    double bY = vertB.Latitude;
    double aX = vertA.Longitude;
    double bX = vertB.Longitude;
    double pY = position.Latitude;
    double pX = position.Longitude;

    if ((aY > pY && bY > pY) | (aY < pY && bY < pY)
            | (aX < pX && bX < pX))
    {
        return false; // a and b can't both be above or below pt.y, and a or
                        // b must be east of pt.x
    }

    double m = (aY - bY) / (aX - bX); // Rise over run
    double bee = (-aX) * m + aY; // y = mx + b
    double x = (pY - bee) / m; // algebra is neat!

    return x > pX;
}

单击“保存”按钮后,将打开一个弹出窗口,允许用户为区域命名,定义要分配的状态,并将点添加到数据库中。点表中有一个ZoneSys列,该列允许在检索点时将其与各自的区域匹配。这是通过UpdateZone命令完成的

public Command<EventArgs> UpdateZone
{
    get
    {
        return new Command<EventArgs>(async e =>
        {
            Zone zone = await App.Database.GetZoneAsync(ActiveZoneID);
            zone.ZoneName = ZoneParameters.ZoneName;
            zone.StatusSys = ZoneParameters.InStatus.StatusSys;
            zone.OutOfZoneStatusSys = ZoneParameters.OutStatus.StatusSys;
            await AddZoneToDBAsync(zone);
            if (MapPage.SaveZoneInfoPopupPageOpen)
            {
                SavePointsOnExit();
                MapPage.SaveZoneInfoPopupPageOpen = false;
            }
        });
    }
}

UpdateZone命令调用SavePointsOnExit方法

private async void SavePointsOnExit()
{
    ActiveZonePoints.OrderBy(o => o.PointOrder);

    for (var i = 0; i < ActiveZonePoints.Count(); i++)
    {
        Model.Point PointToAdd = new Model.Point();
        PointToAdd = ActiveZonePoints[i];
        ActivePointID = await AddPointToDBAsync(PointToAdd);
        PointToAdd.ID = ActivePointID;
        PointToAdd.PointSys = ActivePointID;
        await AddPointToDBAsync(PointToAdd);
    }

    try
    {
        Zone zone = await RetrieveZoneAsync(ActiveZoneID);
    }
    catch
    {
        await Application.Current.MainPage.DisplayActionSheet("no zone returned", "database error", "cancel");
    }
    try
    {
        Zone zone = await RetrieveZoneAsync(ActiveZoneID);
        await CreateZonedPolygonAsync(zone);
    }
    catch
    {
        await Application.Current.MainPage.DisplayActionSheet("Could not create ZonePolygon", "object error", "cancel");
    }


    ActiveZonePoints.Clear();
    ActiveZonePositions.Clear();
    NewZoneOpen = false;
    ClearPins();
    PointCount = 0;
    PopulatePoints();
}

除了将点保存到数据库外,SaveZonePointsOnExit方法还创建ZonePolygon并使用CreateZonedPolygonAsync方法将其添加到可观察的集合中-

private async Task<ZonePolygon> CreateZonedPolygonAsync(Zone zone)
{
    int StatusSys = zone.StatusSys;
    var status = await App.Database.GetStatusBySysAsync(StatusSys);
    int OutStatusSys = zone.OutOfZoneStatusSys;
    var outStatus = await App.Database.GetStatusBySysAsync(OutStatusSys);
    var points = await App.Database.GetZonePointsAsync(zone.ZoneSys);


    ZonePolygon zonePolygon = new ZonePolygon
    {
        ID = zone.ID
    };
    TKPolygon poly = new TKPolygon();
    foreach (Model.Point point in points)
    {
        poly.Coordinates.Add(new Position(point.Latitude, point.Longitude));
    }
    poly.Color = Color.FromHex(status.ColorCode);
    poly.StrokeColor = Color.Firebrick;
    poly.StrokeWidth = 5f;

    _polygons.Add(poly);
    ZonePolygons.Add(zonePolygon);
    return zonePolygon;  
}

到目前为止,所有这些都可以解决。我已经成功创建了第一个多边形。在尝试创建第二个区域之前,我不会遇到任何问题。当我第二次单击AddZone按钮时,效果很好,但是当我单击地图开始创建第二个区域时,将发生nullreference异常。

鉴于第一个区域的创建没有问题,我认为问题一定是由于IsPointInAnyPolygon方法不再立即返回false引起的,因为ZonePolygons列表不再为空。因此,有关从数据库中检索区域以进行检查的问题是问题,或者在创建TKPolygon时可能添加了坐标。我不知道什么有空引用。我会认为,由于我是直接从数据库创建区域,因此所有对象都将被正确保存,并且它们先前的引用将无关紧要。我对此非常困惑。

TL; DR ,CreateZonedPolygonAsync方法或IsPointInAnyPolygon方法存在问题

1 个答案:

答案 0 :(得分:0)

我知道了。我感到很傻,因为我坚持了几个小时,分散了几个星期。继续回到它,无法解决它。问题是在CreateZonedPolygonAsync方法中,我从未将通过点创建的TKPolygon分配给要创建的ZonePolygon对象。因此,当我尝试引用它时不存在。存在的只是ID。真不敢相信我已经错过了这么长时间。

当然,现在我遇到了新的问题,但至少这个问题已解决。

我要做的就是添加zonePolygon.Zpolygon = poly;,如下所示,它现在可以正常工作

private async Task<ZonePolygon> CreateZonedPolygonAsync(Zone zone)
{
    int StatusSys = zone.StatusSys;
    var status = await App.Database.GetStatusBySysAsync(StatusSys);
    int OutStatusSys = zone.OutOfZoneStatusSys;
    var outStatus = await App.Database.GetStatusBySysAsync(OutStatusSys);
    var points = await App.Database.GetZonePointsAsync(zone.ZoneSys);


    ZonePolygon zonePolygon = new ZonePolygon
    {
        ID = zone.ID
    };
    TKPolygon poly = new TKPolygon();
    foreach (Model.Point point in points)
    {
        poly.Coordinates.Add(new Position(point.Latitude, point.Longitude));
    }
    poly.Color = Color.FromHex(status.ColorCode);
    poly.StrokeColor = Color.Firebrick;
    poly.StrokeWidth = 5f;
    zonePolygon.Zpolygon = poly;
    _polygons.Add(poly);

    ZonePolygons.Add(zonePolygon);
    return zonePolygon;  
}