This project has moved and is read-only. For the latest updates, please go here.

CustomTheme giving NullReferenceException Object reference not set to an instance of an object.

Topics: Data Access, SharpMap v0.9 / v1.x
Jan 20, 2012 at 8:41 AM

Im trying to create a theme, but I am getting a  NullReferenceException  Object reference not set to an instance of an object. , when I am calling sharpMap.GetMap(). When im not using my CustomTheme it is working just fine. Can you guys see something I am doing wrong?


Stack Trace:

at SharpMap.Data.Providers.ShapeFile.ExecuteIntersectionQuery(BoundingBox bbox, FeatureDataSet ds) in C:\Projekt\opensource\sharpmap-svn\SharpMap\Data\Providers\ShapeFile.cs:line 561
   at SharpMap.Layers.VectorLayer.RenderInternal(Graphics g, Map map, BoundingBox envelope, ITheme theme) in C:\Projekt\opensource\sharpmap-svn\SharpMap\Layers\VectorLayer.cs:line 256
   at SharpMap.Layers.VectorLayer.Render(Graphics g, Map map) in C:\Projekt\opensource\sharpmap-svn\SharpMap\Layers\VectorLayer.cs:line 242
   at SharpMap.Map.RenderMap(Graphics g) in C:\Projekt\opensource\sharpmap-svn\SharpMap\Map\Map.cs:line 391
   at SharpMap.Map.GetMap() in C:\Projekt\opensource\sharpmap-svn\SharpMap\Map\Map.cs:line 249
   at AutonomForm.Program.Main() in C:\Skole\C#\Autonomus\AutonomForm\Program.cs:line 50
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

This is working:

public void sharpmapAddGreen(String filename, String layername)
            SharpMap.Layers.VectorLayer newLay = new SharpMap.Layers.VectorLayer(layername);
            newLay.DataSource = new SharpMap.Data.Providers.ShapeFile(filename, false);

            newLay.Style.Fill = Brushes.Green;

            //newLay.Style.Outline = new Pen(Color.LightBlue, 1);
            //newLay.Style.Line = new Pen(Color.Red, 1);
            newLay.Enabled = true;
            //newLay.Style.EnableOutline = true;
            //newLay.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;



Not working:

public void sharpmapAddWind(String filenameX, String layername)
            SharpMap.Layers.VectorLayer windLayX = new SharpMap.Layers.VectorLayer(layername + " X");
            windLayX.DataSource = new SharpMap.Data.Providers.ShapeFile(filenameX, false);
            windLayX.Theme = new SharpMap.Rendering.Thematics.CustomTheme(GetWindStyleX);

            windLayX.Enabled = true;

        private SharpMap.Styles.VectorStyle GetWindStyleX(SharpMap.Data.FeatureDataRow row)

            string attributeX = "UGRD";
            SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle();
            byte zeroReference = 256 / 2;
            byte x = (byte)(zeroReference + (Convert.ToSingle((row[attributeX]).ToString())));
            style.Fill = new SolidBrush(Color.FromArgb(0, x, 0, 0));           

            return style;


Jan 20, 2012 at 9:44 AM

You may want to check whether the attribute is DBNull prior to converting the value

Jan 20, 2012 at 10:37 AM

You mean that it may be non-values in that attribute list?

Even if I don't try to access the row data it fails. Like the function below.

        private SharpMap.Styles.VectorStyle GetWindStyleX(SharpMap.Data.FeatureDataRow rows)

            SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle();
            style.Fill = new SolidBrush(Color.FromArgb(0, 100, 0, 0));

            return style;


I can see no reson for it crashing.

Jan 20, 2012 at 11:18 AM

Even this is crashing:

private SharpMap.Styles.VectorStyle GetWindStyleX(SharpMap.Data.FeatureDataRow rows)
            return null;


Jan 20, 2012 at 1:03 PM

Oops, don't you have a dbase file related to your shapefile?

Jan 20, 2012 at 1:41 PM

Thank you so much!!! For you others that is gettign the same problem. Make sure that your build contains the .dbf file that is holding the attribute data if you want to access it.