This project is read-only.

IMapLabelLayer Null Reference Exception

Nov 19, 2010 at 5:27 AM

I'm trying to label my shapefile but keep getting a null reference expeption.  I'm using the example from the published PDF but can't get it to label.  Here is my code snipet that resides in my Form_Load event.  I am using Visual Studio 2010 with .NET 4, DotSpatial.Symbology (1.0.0.13399) and DotSpatial.Symbology.Forms (1.0.0.13401).  My map control (map1) is the DotSpatial.Controls.Map component.  Any direction on this would be greatly appreciated.

            //'-------------------------------------------------
            //' Add shapefile to DotSpatial control
            //'-------------------------------------------------

            string shapefile = ConfigurationManager.AppSettings["shapefile"];
            mFeatureSet = new FeatureSet();

            try
            {
                mFeatureSet.Open(shapefile);
                mFeatureLayer = map1.Layers.Add(mFeatureSet);
                mFeatureLayer.Symbolizer = new PolygonSymbolizer(Color.CadetBlue, Color.DarkBlue);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred loading the shapefile\n" + Strings.ErrorMsg_Critical, "Error Loading Shapefile", MessageBoxButtons.OK, MessageBoxIcon.Error);
                WriteError("frmMain_Load", ex.Message);
            }

            //' Label the plots with the plot number
            IMapLabelLayer labelLayer = new MapLabelLayer(mFeatureSet);
            labelLayer.CreateLabels();
            ILabelCategory category = labelLayer.Symbology.Categories[0];
            category.Expression = "[plot_nbr]";
            category.Symbolizer.Orientation =
            ContentAlignment.MiddleCenter;
            mFeatureLayer.ShowLabels = true;
            mFeatureLayer.LabelLayer = labelLayer;  //' Null reference exception thrown here //

Jan 21, 2011 at 7:46 PM

I got labels to work after much debugging and stumbling through source code.  Here is what worked for me:

        //'========================================================
        //' PRIVATE VARIABLES 
        //'========================================================

        private IMapPolygonLayer mFeatureLayer;

private void frmMain_Load(object sender, EventArgs e) { //'------------------------------------------------- //' Add shapefile to DotSpatial control //'------------------------------------------------- //' Get the shapefile path from config string shapefile = ConfigurationManager.AppSettings["shapefile"]; //' Create a featureset object and fill from the shapefile FeatureSet featureSet = new FeatureSet(FeatureType.Polygon); featureSet.Open(shapefile); //' Initialize the polygonLayer object and add to the Map control mFeatureLayer = new MapPolygonLayer(featureSet); map1.Layers.Add(mFeatureLayer);
//'------------------------------------------------- //' Label polygons //'------------------------------------------------- this.LabelPolygons(featureSet); }
        //'---------------------------------------------------------
        //' METHOD:                                        
        //'---------------------------------------------------------

        private void LabelPolygons(IFeatureSet pFeatureSet)
        {
            //' This was required because an error was being thrown at: DotSpatial.Data.FeatureSet.SelectIndices at line 1141
            //' Error was: Object reference not set to an instance of an object.
            //' Running CopyFeatures worked and populated the variable that was not previously set when the input
            //' featureset (pFeatureSet) was created on Form_Load from the shapefile.
            IFeatureSet featureSet = new FeatureSet(FeatureType.Polygon);
            featureSet.CopyFeatures(pFeatureSet, true);
           
            //' Create a new instance of IMapLabelLayer using the new featureset
            IMapLabelLayer labelLayer = new MapLabelLayer(featureSet);

            //' Label the plots with the plot number
            ILabelCategory category = labelLayer.Symbology.Categories[0];
            category.Expression = "[plot_nbr]";
            category.Symbolizer.Orientation = ContentAlignment.MiddleCenter;
            category.Name = "id";

            mFeatureLayer.LabelLayer = labelLayer;
            mFeatureLayer.ShowLabels = true;
        }


Jan 21, 2011 at 7:50 PM

re-posting Form_Load handler:

        private void frmMain_Load(object sender, EventArgs e)
        {
            //'-------------------------------------------------
            //' Add shapefile to DotSpatial control
            //'-------------------------------------------------

            //' Get the shapefile path from config
            string shapefile = ConfigurationManager.AppSettings["shapefile"];

            //' Create a featureset object and fill from the shapefile
            FeatureSet featureSet = new FeatureSet(FeatureType.Polygon);
            featureSet.Open(shapefile);

            //' Initialize the polygonLayer object and add to the Map control
            mFeatureLayer = new MapPolygonLayer(featureSet);
            map1.Layers.Add(mFeatureLayer);
            ProjectionInfo pInfo = map1.Projection;

            //'-------------------------------------------------
            //' Symbolize the polygons
            //'-------------------------------------------------
            this.SymbolizePolygons(featureSet);

            //'-------------------------------------------------
            //' Label polygons
            //'-------------------------------------------------
            this.LabelPolygons(featureSet);
        }


 

Jan 25, 2011 at 10:28 AM

Thank you eklundjb, for your quick fix!
Do you know how do you hide the painted dot, when labling a point layer?

 

Jan 25, 2011 at 4:21 PM
mFeatureLayer.Symbolizer = new PointSymbolizer(Color.Transparent, DotSpatial.Symbology.PointShape.Diamond, 1);

This works, but setting the Size parameter to 0 generates an unhandled exception! 
Jan 25, 2011 at 4:27 PM

I'm trying to do the same with a spatialite dataset but all that shows on the map is [Field_Name] on 3 of the 5 polygons.

My LabelPolygons is the same as above except I pass in the [column name] and [IMapLayer myLayer] to set the myLayer.labelLayer and set myLayer.ShowLabels = true;

Any ideas will be appreciated.

 

------------------ADD THE LAYER TO THE MAP

string connString = "Data Source=" + dbConnectionString;
SpatiaLiteVectorProvider slProv = new SpatiaLiteVectorProvider();
IDataSet ds = slProv.Open(connString, tableName, uID, geomField);
IMapFeatureLayer myLayer = map1.Layers.Add(ds) as IMapFeatureLayer;

------------------CREATE THE FEATURE SET

SQLiteDataAdapter mySqlAdapter = new SQLiteDataAdapter("SELECT * FROM " + tableName, connString);
System.Data.DataSet myDataSet = new System.Data.DataSet();
mySqlAdapter.Fill(myDataSet);
DataTable myDataTable = myDataSet.Tables[0];
FeatureSet featureSet = new FeatureSet(FeatureType.Polygon);
featureSet.DataTable = myDataTable;

---------------------ADD THE LABELS

this.LabelPolygons(myLayer,featureSet, "Field_Name");

 

 

Jan 25, 2011 at 4:40 PM

It turns out the column name is case sensitive.  When I pass in "FIELD_NAME" the labels show on all five polygons.

Thanks for the example to work with.

Joe

Feb 17, 2011 at 9:51 PM
I don't, I just starting developing with DotSpatial and have only been working with polygon layers currently.


On Tue, Jan 25, 2011 at 2:28 AM, ce_web wrote:

From: ce_web
Thank you eklundjb, for your quick fix!
Do you know how do you hide the painted dot, when labling a point layer?
To add a post to this discussion, reply to this email ( DotSpatial@discussions.codeplex.com)
To start a new discussion for this project, email DotSpatial@discussions.codeplex.com
You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribeon CodePlex.com.
Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com