error when saving shape

Oct 22, 2010 at 9:58 PM
Edited Oct 23, 2010 at 8:50 PM


"fs.SaveAs(SaveFileDialog1.FileName, True)"

worked earlier, but after upgrading to recent release I received an error message:

"ArgumentNullException was unhandled

Path cannot be null.
Parameter name: path"

"MsgBox(SaveFileDialog1.FileName.ToString())" shows that the path is exactly like earlier, when the code was working. Any idea how to rectify this problem?

Even this results the same error message: "fs.SaveAs("C:\test.shp", True)"

Oct 24, 2010 at 6:21 AM

Did some testing and it looks like it is the featureset which is causing the problem. I haven't changed any code, something in that recent upgrade is causing it.

Oct 24, 2010 at 3:27 PM

Ok, let me do some testing and check.  It should be an easy fix since it just looks like an unhandled null exception, possibly with a projection that is null or something.

Oct 25, 2010 at 12:44 AM


I tested with a projection, result was the same.

Oct 28, 2010 at 11:52 PM

I tested with DotSpatial sample code,"Multi Line Feature Set", one to one, and I got exactly same error message.

Oct 29, 2010 at 9:15 PM

This works:

        Dim fs As New FeatureSet()
        fs.SaveAs("C:\test_new.shp", True)


But this doesn't:

        Dim c As Coordinate() = New Coordinate(49) {}
        Dim rnd As New Random()
        Dim f As New Feature()
        Dim fs As New FeatureSet(f.FeatureType)
        For i As Integer = 0 To 49
            c(i) = New Coordinate((rnd.[Next](0, 50) + 360) - 90, (rnd.NextDouble() * 360) - 180)
        fs.SaveAs("C:\sika_test.shp", True)

I have tried various different ways to create a featureset, but always received same error message. Is there something clearly wrong or is there something I could try to do to rectify this problem?

Nov 9, 2010 at 4:26 AM
Edited Nov 9, 2010 at 5:19 AM

I started a new project from the scratch and put only a simple code to create a featureset and save as a shape file. First time I ran the code, the software tried to open AttributeTable.cs file/form, the dialog opened, title was: "Find Source: AttributeTable.cs", and Original location: "C:\Dev\DotSpatial\DotSpatial.Data\DotSpatial.Data\AttributeTable.cs". Didn't have that form... and next time when running the code, it didn't try to open that file, but eventually got that initial error message indicated in my first post. I can create a featureset and present the shapes, but can't save the shape file.

Nov 9, 2010 at 3:02 PM

Ok, so there must be a bug in the AttributeTable.  This is not a form.  When you trigger an exception, the portable debug file tries to take you directly to the line in the source code, but in your case you don't have the source code, or it's in a different folder location, and so the effort to open the debugger fails.  I will try to replicate your task this evening and see if I can trip the same exception.  Thanks for posting this.

Nov 9, 2010 at 10:59 PM
Edited Nov 9, 2010 at 11:00 PM

Ok, so I got the source code and started all over again. The first error message:

Source file: (path)\AttributeTable.cs

Module: (path)\DotSpatial.Data.dll

Process: [5756] (project name).vshost.exe

The source file is different from when the module was built. Would you like the debugger to use it anyway:

I chose "Yes" and let software open that AttributeTable.cs file. Debugger pointed to the line:

FileInfo fi = new FileInfo(_fileName); (in private void GetRowOffsets())

and the error message was:

System.ArgumentNullException was unhandled
Message=Path cannot be null.
Parameter name: path

Without that featureset.saveas shapes will be drawn OK, but if I add it into the code, that will result error.

Nov 10, 2010 at 3:48 AM

Just realized that I downloaded that source code from different release. So with the correct source code, the debugger indicated the following line:

FileStream myStream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 100000);

Error messages were same as earlier.

Nov 13, 2010 at 3:15 PM

I believe this is fixed in changeset  e2cb87c0818b


The problem was when you passed file name to FeatureSet.Save() it was expected to have a full path.  The full path is now inferred by GetFullPath()

Nov 13, 2010 at 9:27 PM
Edited Nov 13, 2010 at 10:25 PM

It did fix that fs.SaveAs problem, thank you.

Now I got couple of other problems, e.g. labels don't work, but it could be due to that I built only DotSpatial.Data.dll. I didn't receive any error messages though, I'll try to build the whole project and see if that rectifies those other problems.

Compiled everything I could, but still got some problems. Labels were not drawn, because datatable was empty and when trying to save the map, got an error message "The type initializer for 'DotSpatial.Serialization.SerializationMap' threw an exception". I better wait that DLL release package, not quite sure how to build the complete project.

Nov 15, 2010 at 3:20 AM

I downloaded 14-11-2010 release and source code and tried again. I tested with that multi point feature set sample code, didn't work, now debugger opens ShapefileHeader.cs file and points into the line:

_xMin = extent.MinX; (in public void SetExtent(IExtent extent))

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.

Nov 22, 2010 at 6:40 AM

21-11-2010 release seemed to fix those minor problems.

Nov 24, 2010 at 4:41 AM

I'm still having an issue with saving a shapefile. The shapefile gets saved and can be opened in the map, but the attribute table has zero rows.

If I try to open the attribute table in DotSpatial or open the .dbf file in Excel, there are no rows (only the column names). Also, for some reason

there are two columns "FID" and "FID0" in the table. Here is my test code using today's build of DotSpatial:

        private void CreateNewShapefile()
            Random rnd = new Random();
            // create feature set..
            FeatureSet fs = new FeatureSet(FeatureType.Point);
            DataTable tab = fs.DataTable;
            tab.Columns.Add(new DataColumn("SiteName", typeof(string)));

            // add some points
            for (int i = 0; i < 10; i++)
                Coordinate coord = new Coordinate(180 * rnd.NextDouble(), 90 * rnd.NextDouble());

                Feature f = new Feature(FeatureType.Point, new Coordinate[] { coord });

                //populate the attribute
                DataRow row = f.DataRow;
                row["SiteName"] = "Site " + i;

            //set projection and save the shapefile
            fs.Projection = DotSpatial.Projections.KnownCoordinateSystems.Geographic.World.WGS1984;
            fs.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
                Path.DirectorySeparatorChar + "test.shp", true);

Is this a bug, or is there anything wrong I'm doing in my code?

Is there any better way of programmatically creating a shapefile with attributes in DotSpatial?

Nov 24, 2010 at 5:10 AM

I examined the DotSpatial source code and added a line of code to the SaveAs() method in FeatureSet.cs on line 983:

if (AttributesPopulated) 
   result.DataTable = DataTable;    
   result.AttributesPopulated = true; //Newly added code 

This seems to fix my issue. However, the logic of FeatureSet is quite complex (index mode, internal dataset..) so I'd like to test any side-effects before committing this change.

Nov 24, 2010 at 6:07 AM

I noticed the same thing, couldn't add attributes. I'll try your fix, if that helps in my case. Thanks for the tip.

Mar 10, 2014 at 9:21 PM
Edited Mar 10, 2014 at 9:52 PM
I downloaded 10-03-2014 release and source code.

But this bug continues. I tested with that multi point feature set sample code, debugger opens ShapefileHeader.cs file and points into the line:

_xMin = extent.MinX; (in public void SetExtent(IExtent extent))

Message=Object reference not set to an instance of an object.

My code is:
  using DotSpatial.Data;
  using DotSpatial.Topology; //instead of DotSpatial.Geometries

             MultiPoint mp = new MultiPoint(vertices);
             Feature f = new Feature(mp);
             FeatureSet fs = new FeatureSet(f.FeatureType);
             fs.Name = tiposF.Nombre;
             fs.Projection = dest;
             fs.SaveAs(facilidadesSistema, true);//<- Here the error
The source code in ShapefileHeader.cs:
         if (AttributesPopulated)
            result.DataTable = DataTable;

            //added by Jiri Kadlec to prevent overwriting the DataTable in the Save() function
            result.AttributesPopulated = true;
However, issue is still displayed
Thanks for your support.
Mar 11, 2014 at 8:34 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Mar 11, 2014 at 8:50 AM
Hi calvimor,
thank you for posting this issue. Now it's fixed. You can try it with latest source code.

Marked as answer by jany_ on 9/10/2014 at 1:09 PM