Exception"Object reference not set to an instance of an object." when implementing sf.ExecuteIntersectionQuery(bb, fds);

Topics: SharpMap v0.9 / v1.x
Sep 13, 2013 at 2:16 AM
Hello everyone:
I have encountered an exception when I implement SharpMap.ExecuteIntersectionQuery(). Generally, I want to select a feature by its attribute. After referencing to some topics I write these code:
public int SelectFeatureAttrib(VectorLayer VLayer,string owner)
    {

        string attrib = "ID";

        string path = VLayer.DataSource.ConnectionID;    //shapefile路径
        ShapeFile sf = new ShapeFile(path);
        Envelope bb = VLayer.Envelope;
        sf.FilterDelegate = delegate(SharpMap.Data.FeatureDataRow row)
        {
            if (row[attrib].Equals(owner))
            {
                return true;
            }
            else
            {
                return false;
            }
        };
        var fds = new SharpMap.Data.FeatureDataSet();
        sf.ExecuteIntersectionQuery(bb, fds);
        return -1;
    }  
In fact, the VLayer is a vectorlayer opened in MapBox. When implementing in "sf.ExecuteIntersectionQuery(bb, fds);", visual studio points to Shapefile.cs
var res = _tree.Search(bbox);
The _tree is empty and i find out this variable is declared as QuadTree. I check and find *.sidx file. Could some one help me find out the reason? Many thanks!
Sep 13, 2013 at 4:00 AM
I used another code to solve this problem, even thought I still don't know the reason. But at least I can query the feature by it's ID. Here is the code :
     public SharpMap.Data.FeatureDataRow SelectFeatureAttrib(VectorLayer VLayer, string owner)
    {
        SharpMap.Data.Providers.ShapeFile shapeProvider = (SharpMap.Data.Providers.ShapeFile)VLayer.DataSource;
        shapeProvider.FilterDelegate = delegate(SharpMap.Data.FeatureDataRow row)
        {
            return Convert.ToString(row["ID"]) == owner;
        };
        SharpMap.Data.FeatureDataSet ds = new SharpMap.Data.FeatureDataSet();
        VLayer.DataSource.ExecuteIntersectionQuery(VLayer.Envelope, ds);
        VLayer.DataSource.Close();
        shapeProvider.FilterDelegate = null;
        if (ds.Tables.Count > 0)
        {
            SharpMap.Data.FeatureDataTable tbl = ds.Tables[0] as SharpMap.Data.FeatureDataTable;
            return (SharpMap.Data.FeatureDataRow)tbl.Rows[0];
        }
        else
        {
            return null;
        }

    }
Coordinator
Sep 13, 2013 at 7:59 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.