ThinkGeo.com    |     Documentation    |     Premium Support

V13 NullReferenceException when adding overlay to mapView

Hello there,

I am kind of new to ThinkGeo and just got to work recently on a project using ThinkGeo v5.
I followed the different guides and wiki and manages to upgrade to the latest nuGet packages 13.2.1 (and 13.2.2 for ThinkGeo.UI.Winforms).

What I wanted to do in my application was to change the map provider and instead of using shape file I’d use a WMS service. So basically I got rid of the shape file loading and looked at the sample provided here.

Here are some code samples from my project:
MapComponent.cs
public MapComponent()
{
InitializeComponent();
localizeComponent();
mapView.CurrentExtentChanged +=
new EventHandler(MapView_CurrentExtentChanged);
JMSManager.getInstance().OnUnitStatusUpdate += new JMSManager.UnitEventHandler(MapComponent_OnUnitStatusUpdate);
initMap();
zoomToFullExtent();

    mapView.TrackOverlay.TrackEnded += new EventHandler<TrackEndedTrackInteractiveOverlayEventArgs>(trackOverlay_TrackEnded);
    mapView.TrackOverlay.TrackEnding += new EventHandler<TrackEndingTrackInteractiveOverlayEventArgs>(trackOverlay_TrackEnding);
    mapView.TrackOverlay.TrackStarting += new EventHandler<TrackStartingTrackInteractiveOverlayEventArgs>(trackOverlay_TrackStarting);
    //createSignalAreaShapefile();
    initAppVersion();
}
**[...]**
public async void initMap()
{
    mapView.MapUnit = GeographyUnit.DecimalDegree;
    
    // Clear out the overlays so we start fresh
    mapView.Overlays.Clear();

    // Create an overlay that we will add the layer to.
    LayerOverlay staticOverlay = new LayerOverlay();
    mapView.Overlays.Add(staticOverlay);

    // Create the WMS layer using the parameters below.
    // This is a public service and is very slow most of the time.
    ThinkGeo.Core.Async.WmsRasterLayer wmsImageLayer = new ThinkGeo.Core.Async.WmsRasterLayer(new Uri("http://ows.mundialis.de/services/service"));
    wmsImageLayer.ActiveLayerNames.Add("OSM-WMS");
    wmsImageLayer.ActiveStyleNames.Add("default");
    wmsImageLayer.Exceptions = "application/vnd.ogc.se_xml";

    // Add the layer to the overlay.
    staticOverlay.Layers.Add("wmsImageLayer", wmsImageLayer);



    mapView.CurrentExtent = new RectangleShape(-96.8538765269409, 33.1618647290098, -96.7987487018851, 33.1054126590461);
    await mapView.RefreshAsync();
    startGPS();
}

MapComponen.designer.cs
private System.Windows.Forms.Panel mainPanel;
private System.Windows.Forms.Panel mapPanel;
private ThinkGeo.UI.WinForms.MapView mapView;

**[...]**

private void InitializeComponent()
{
    this.mapView = new ThinkGeo.UI.WinForms.MapView();
    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MapComponent));
    this.mainPanel = new System.Windows.Forms.Panel();
    this.mapPanel = new System.Windows.Forms.Panel();
    this.infoTableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
    this.speedLabelControl = new DevExpress.XtraEditors.LabelControl();
    this.countLabelControl = new DevExpress.XtraEditors.LabelControl();
    this.stopSimpleButton = new DevExpress.XtraEditors.SimpleButton();
    this.zoomLevelLabelControl = new DevExpress.XtraEditors.LabelControl();
    this.longitudeLabelControl = new DevExpress.XtraEditors.LabelControl();
    this.latitudeLabelControl = new DevExpress.XtraEditors.LabelControl();
    this.scaleLabelControl = new DevExpress.XtraEditors.LabelControl();
    this.scaleValueLabelControl = new DevExpress.XtraEditors.LabelControl();
    this.eastingValueLabelControl = new DevExpress.XtraEditors.LabelControl();
    this.northingValueLabelControl = new DevExpress.XtraEditors.LabelControl();
    this.routeTrackBarControl = new DevExpress.XtraEditors.TrackBarControl();
    this.playSimpleButton = new DevExpress.XtraEditors.SimpleButton();
    this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
    this.labelControl1 = new DevExpress.XtraEditors.LabelControl();
    this.labelControl2 = new DevExpress.XtraEditors.LabelControl();
    this.mainPanel.SuspendLayout();
    this.mapPanel.SuspendLayout();
    this.infoTableLayoutPanel.SuspendLayout();
    ((System.ComponentModel.ISupportInitialize)(this.routeTrackBarControl)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.routeTrackBarControl.Properties)).BeginInit();
    this.tableLayoutPanel2.SuspendLayout();
    this.SuspendLayout();
    // 
    // mainPanel
    // 
    this.mainPanel.Dock = System.Windows.Forms.DockStyle.Fill;
    this.mainPanel.Location = new System.Drawing.Point(0, 0);
    this.mainPanel.Name = "mainPanel";
    this.mainPanel.Size = new System.Drawing.Size(800, 600);
    this.mainPanel.TabIndex = 0;
    // 
    // mapPanel
    // 
    this.mapPanel.Dock = System.Windows.Forms.DockStyle.Fill;
    this.mapPanel.Location = new System.Drawing.Point(0, 0);
    this.mapPanel.Name = "mapPanel";
    this.mapPanel.Size = new System.Drawing.Size(800, 600);
    this.mapPanel.TabIndex = 0;
    // 
    // MapView
    // 
    this.mapView.BackColor = System.Drawing.Color.White;
    this.mapView.CurrentScale = 0D;
    this.mapView.Dock = System.Windows.Forms.DockStyle.Fill;
    this.mapView.Location = new System.Drawing.Point(0, 0);
    this.mapView.MapFocusMode = ThinkGeo.Core.MapFocusMode.Default;
    this.mapView.MapResizeMode = ThinkGeo.Core.MapResizeMode.PreserveExtent;
    this.mapView.MaximumScale = 80000000000000;
    this.mapView.MinimumScale = 200;
    this.mapView.RestrictExtent = null;
    this.mapView.Name = "mapView";
    this.mapView.Size = new System.Drawing.Size(800, 572);
    this.mapView.TabIndex = 0;
    this.mapView.Text = "mapView";
    //this.mapView.SizeChanged += new System.EventHandler(this.MapView_SizeChanged);
    //this.mapView.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MapView_MouseMove);
    //this.mapView.MapClick += new System.EventHandler<ThinkGeo.Core.MapClickMapViewEventArgs>(this.MapView_MapClick);
    **[...]**
    // 
    // MapComponent
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.mapPanel.Controls.Add(this.mapView);
    this.mapPanel.Controls.Add(this.infoTableLayoutPanel);
    this.Controls.Add(this.mainPanel);
    this.Name = "MapComponent";
    this.Size = new System.Drawing.Size(800, 600);
    this.mainPanel.ResumeLayout(false);
    this.mapPanel.ResumeLayout(false);
    this.infoTableLayoutPanel.ResumeLayout(false);
    ((System.ComponentModel.ISupportInitialize)(this.routeTrackBarControl.Properties)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.routeTrackBarControl)).EndInit();
    this.tableLayoutPanel2.ResumeLayout(false);
    this.ResumeLayout(false);

}

I tried taking only the relevant parts of the code, please do tell me if something is missing.

My issue is that the project crashes when initMap() is called especially when adding the overlay here:
mapView.Overlays.Add(staticOverlay);

Here’s the error:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=ThinkGeo.UI.WinForms
StackTrace:
at ThinkGeo.Core.MapViewBase.3kc=(Object sender, AddedGeoCollectionEventArgs e)

I have looked at many topics on this forum about the same subject but most of them were about WPF. I tried multiple solutions with no success. I tried adding a currentExtent before adding the overlay but it was always out of bounds no matter what. I tried checking my mapView and overlay before adding it but none of them are null.

Would you happen to have any guidance on how to debug this ? I am available if any other information is needed. I have been looking for solutions for two days now so this is my last resort.

Regards.

Hi @DrGouh,

It’s hard to tell your exact issue without access to the project. Can you upload the entire .sln project, or email it to support@thinkgeo.com?

Thanks,
John

Hello,

Thanks for your answer. Sadly I cannot upload the whole project nor send it. It contains information I dont want to share sorry.

Regards.

Hi @DrGouh,

I can confirm that your code in initMap() works fine when it’s inside of a traditional Form_Load function. I think the issue is somehow triggered on how your MapComponent.cs is being loaded. There could also be some issues in your other code or DevExpress components.

If you’re unable to send us your whole .sln, are you able to reproduce the error in a simpler pared down .sln that you can send for us to review? Also, sometimes when trying to break down the error to a simpler reproducible form it will become apparent where the conflict is occurring. Or if not, is there any way you can get the full stack trace?

Thanks,
John

Thanks,
John

1 Like

Thanks a lot for your help. I wa sable to get the map working properly by using the Form_Load and adding it to my Designer class. I guess the issue was just the overall order of execution which was a bit messy.

Now just to clear out any confusion regarding the samples provided. I have a map server I’d like to use as a single layer for the base map. Is it better to use the Overlay or the Layer ? I’m not sure to understand the difference between using the WmsOverlay and the LayerOverlay.
I’ve used the LayerOverlay for now but it seems I cannot move by clicking and dragging on the map, I can zoom and double click to zoom though.

I think I just need a bit of theory please.

Again, thanks a lot for your helpful support!

Hi @DrGouh,

I’m glad you found a solution for the loading order issue.

You can find a good overview of the differences between an overlay and a layer in this blog post. But in short, an overlay is a collection of layers that can be grouped together.

Using a LayerOverlay should not prevent the map from drag/panning. I would guess that there’s still an issue with the way all of your legacy v5 classes were upgraded. You may want to try rebuilding your legacy app one layer at a time until you find the issue. Converting a large application all at once can sometimes be harder to debug. I’ll also have Phil reach out to you and offer some direct support.

Thanks,
John