# Null Reference calling GetFeaturesByColumnValue on custom InMemoryFeatureSource

**URL:** <https://community.thinkgeo.com/t/null-reference-calling-getfeaturesbycolumnvalue-on-custom-inmemoryfeaturesource/405>\
**Category:** WinForms\
**Created:** [May 8, 2009, 9:32am UTC](https://community.thinkgeo.com/t/null-reference-calling-getfeaturesbycolumnvalue-on-custom-inmemoryfeaturesource/405 "2009-05-08T09:32:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/b/50afbb/32.png) [@Brian1](https://community.thinkgeo.com/u/Brian1)\
**Post date:** [May 8, 2009, 9:32am UTC](https://community.thinkgeo.com/t/null-reference-calling-getfeaturesbycolumnvalue-on-custom-inmemoryfeaturesource/405/1 "2009-05-08T09:32:06Z")

</div>

Hi,

I&nbsp;created&nbsp;a&nbsp;custom&nbsp;in&nbsp;memory&nbsp;feature&nbsp;source&nbsp;and&nbsp;layer&nbsp;to&nbsp;wrap&nbsp;my&nbsp;own&nbsp;layer&nbsp;objects&nbsp;from&nbsp;a&nbsp;custom&nbsp;spatial&nbsp;storage&nbsp;system.&nbsp;&nbsp;I&nbsp;have&nbsp;no&nbsp;problems&nbsp;displaying&nbsp;the&nbsp;layer&nbsp;in&nbsp;a&nbsp;map&nbsp;control&nbsp;and&nbsp;using&nbsp;styles/labels&nbsp;that&nbsp;access&nbsp;the&nbsp;attributes&nbsp;of&nbsp;the&nbsp;features.

But&nbsp;when&nbsp;I&nbsp;tried&nbsp;to&nbsp;call&nbsp;FeatureSource.GetFeaturesByColumnValue&nbsp;I&nbsp;get&nbsp;a&nbsp;NullReferenceException

System.NullReferenceException&nbsp;occurred  
  
&nbsp;&nbsp;Message="Object&nbsp;reference&nbsp;not&nbsp;set&nbsp;to&nbsp;an&nbsp;instance&nbsp;of&nbsp;an&nbsp;object."  
  
&nbsp;&nbsp;Source="MapSuiteCore"  
  
&nbsp;&nbsp;StackTrace:  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.Core.FeatureSource.GetFeaturesByColumnValueCore(String&nbsp;columnName,&nbsp;String&nbsp;columnValue)  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.Core.FeatureSource.GetFeaturesByColumnValue(String&nbsp;columnName,&nbsp;String&nbsp;columnValue)  
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;Tcc.Expresso.UI.RecEngine.VRAExportControl.listViewPrevew\_SelectionChanged(Object&nbsp;sender,&nbsp;SelectionChangedEventArgs&nbsp;e)&nbsp;in&nbsp;C:\TFSSource\Expresso\Tcc.Expresso.UI\RecEngine\VRAExportControl.xaml.cs:line&nbsp;273  
  
&nbsp;&nbsp;InnerException:&nbsp;  
  
&nbsp;

In&nbsp;my&nbsp;custom&nbsp;feature&nbsp;source&nbsp;I&nbsp;did&nbsp;provide&nbsp;it&nbsp;with&nbsp;the&nbsp;List&nbsp;of&nbsp;FeatureSourceColumn&nbsp;and&nbsp;set&nbsp;the&nbsp;attribute&nbsp;dictionary&nbsp;of&nbsp;each&nbsp;feature.&nbsp;&nbsp;I&nbsp;assume&nbsp;that&nbsp;I&nbsp;have&nbsp;not&nbsp;set&nbsp;something&nbsp;I&nbsp;need&nbsp;to,&nbsp;any&nbsp;pointer&nbsp;in&nbsp;the&nbsp;right&nbsp;direction&nbsp;would&nbsp;be&nbsp;great!

Thanks

Brian

---

<div class="post-metadata">

**Author:** ![Lee](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@Lee](https://community.thinkgeo.com/u/Lee)\
**Post date:** [May 12, 2009, 4:35am UTC](https://community.thinkgeo.com/t/null-reference-calling-getfeaturesbycolumnvalue-on-custom-inmemoryfeaturesource/405/2 "2009-05-12T04:35:21Z")

</div>

Brian,

I&nbsp;tried&nbsp;to&nbsp;recreate&nbsp;the&nbsp;issue&nbsp;by&nbsp;simply&nbsp;write&nbsp;MyInMemoryFeatureSource&nbsp;but&nbsp;failed.

`private void button1_Click(object sender, EventArgs e){    MyInMemoryFeatureSource source = new MyInMemoryFeatureSource();    source.Open();    Feature feature = source.GetFeaturesByColumnValue("Name", "A");}public class MyInMemoryFeatureSource : InMemoryFeatureSource{    public MyInMemoryFeatureSource()    	: base(new List<FeatureSourceColumn>())    {    }}`

Could&nbsp;you&nbsp;provide&nbsp;some&nbsp;code&nbsp;to&nbsp;help&nbsp;on&nbsp;recreating&nbsp;this&nbsp;issue,&nbsp;it&nbsp;will&nbsp;be&nbsp;great&nbsp;if&nbsp;there&nbsp;is&nbsp;a&nbsp;demo&nbsp;about&nbsp;it.

Thanks,  
  
ThinkGeo&nbsp;Support&nbsp;  
  
&nbsp;

---

<div class="post-metadata">

**Author:** ![Brian1](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/b/50afbb/32.png) [@Brian1](https://community.thinkgeo.com/u/Brian1)\
**Post date:** [May 12, 2009, 8:26am UTC](https://community.thinkgeo.com/t/null-reference-calling-getfeaturesbycolumnvalue-on-custom-inmemoryfeaturesource/405/3 "2009-05-12T08:26:59Z")

</div>

Hi&nbsp;Lee,

I've&nbsp;included&nbsp;the&nbsp;code&nbsp;for&nbsp;my&nbsp;custom&nbsp;Feature&nbsp;Source&nbsp;so&nbsp;you&nbsp;can&nbsp;see&nbsp;what&nbsp;I'm&nbsp;doing,&nbsp;I&nbsp;can&nbsp;probably&nbsp;put&nbsp;together&nbsp;a&nbsp;simple&nbsp;app&nbsp;if&nbsp;needed&nbsp;also&nbsp;a&nbsp;little&nbsp;later.

Brian

&nbsp;

`using System;using System.Collections.Generic;using System.Linq;using System.Text;using ThinkGeo.MapSuite.Core;using Tcc.SpatialStorage;namespace Tcc.Gis.ThinkGeoSupport{    public class TccFeatureSource : InMemoryFeatureSource    {        #region Construct/Initialize        public TccFeatureSource(ITccFeatureLayer layerToWrap):            base(CreateFeatureSourceColumnsForITccFeatureLayer(layerToWrap))        {            this.OpenedFeatureSource += new EventHandler<OpenedFeatureSourceEventArgs>(TccFeatureSource_OpenedFeatureSource);            this.WrappedLayer = layerToWrap;        }        private void TccFeatureSource_OpenedFeatureSource(object sender, OpenedFeatureSourceEventArgs e)        {            AddTccFeaturesToInMemoryFeatures();        }        #endregion        #region Public Properties        public ITccFeatureLayer WrappedLayer { get; private set; }        #endregion        #region Private Methods        private void AddTccFeaturesToInMemoryFeatures()        {            //We need to remove any projection while we are adding features            //Otherwise the lat/lon tcc feature WKB will be un-projected incorrectly            var tempProjection = Projection;            Projection = null;            this.BeginTransaction();            foreach( ITccFeature tccFeature in WrappedLayer.TccFeatures )            {                var attributeDictionary = new Dictionary<string, string>();                foreach( ILayerColumn layerColumn in WrappedLayer.Columns )                    attributeDictionary.Add(layerColumn.Name, tccFeature.GetAttribute(layerColumn).ToString());                var thinkGeoFeature = new Feature(tccFeature.WellKnownBinary, tccFeature.Index.ToString(), attributeDictionary);                AddFeature(thinkGeoFeature);            }            this.CommitTransaction();            //Now that the transaction had been committed, we can set the projection again            Projection = tempProjection;        }        #endregion        #region Static Methods        public static List<FeatureSourceColumn> CreateFeatureSourceColumnsForITccFeatureLayer(ITccFeatureLayer layer)        {            var outlist = new List<FeatureSourceColumn>();            foreach( ILayerColumn tccColumn in layer.Columns )                outlist.Add(new FeatureSourceColumn(tccColumn.Name, tccColumn.DataType.ToString(), tccColumn.Length));            return outlist;        }        #endregion    }}`

---

<div class="post-metadata">

**Author:** ![Lee](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/l/bc8723/32.png) [@Lee](https://community.thinkgeo.com/u/Lee)\
**Post date:** [May 14, 2009, 9:23am UTC](https://community.thinkgeo.com/t/null-reference-calling-getfeaturesbycolumnvalue-on-custom-inmemoryfeaturesource/405/4 "2009-05-14T09:23:09Z")

</div>

Brian,

I&nbsp;have&nbsp;recreated&nbsp;it&nbsp;and&nbsp;this&nbsp;is&nbsp;an&nbsp;issue&nbsp;in&nbsp;our&nbsp;system,&nbsp;we&nbsp;have&nbsp;added&nbsp;it&nbsp;to&nbsp;our&nbsp;tracking&nbsp;system&nbsp;and&nbsp;it&nbsp;will&nbsp;be&nbsp;fixed&nbsp;it&nbsp;in&nbsp;the&nbsp;upcoming&nbsp;version&nbsp;which&nbsp;will&nbsp;be&nbsp;release&nbsp;around&nbsp;next&nbsp;week.

For&nbsp;now&nbsp;you&nbsp;can&nbsp;override&nbsp;GetFeaturesByColumnValueCore&nbsp;&nbsp;method&nbsp;to&nbsp;avoid&nbsp;this&nbsp;issue&nbsp;for&nbsp;now.

`protected override Collection<Feature> GetFeaturesByColumnValueCore(string columnName, string columnValue){    Collection<Feature> returnFeatures = new Collection<Feature>();    Collection<Feature> features = GetAllFeatures(new string[] { columnName });    for (int i = 0; i < features.Count; i++)    {        if (features[i].GetWellKnownBinary() != null && String.CompareOrdinal(features[i].ColumnValues[columnName], columnValue) == 0)        {            returnFeatures.Add(features[i]);        }    }	return returnFeatures;}`

ThinkGeo&nbsp;Support
