Possible System.NullReferenceException false positive

I thought I would mention that I was converting an automatic property to a property with a backing field and finally to just a public Setter method when much to my surprise I got a warning on a possible null usage on the following code.



            if (_activeTube == null)
            {
                if (theTubeLevel == null) return ....
                if (theTubeLevel.Tube == null) return ...//this can be commented out too when not using Setter
                SetActiveTube(theTubeLevel.Tube);  //Comment this line out and
                //_activeTube = theTubeLevel.Tube;  //Use this instead to avoid warning
            }
            ... = _activeTube.Feature....  //possible null exception here on use of _activeTube



 



The Setter method is just

        public void SetActiveTube(Tube value)
        {
            _activeTube = value;
        }



 


when the code used an automatic property and  was
   ... = ActiveTube().Feature.....
there was no null exception warning

If I replace the public SetActiveTube() call above by directly using the field directly as shown in the comment all is well. Also interesting is that when I directly set the field I no longer need to test theTubeLevel.Tube against null (which is OK because the application can't have a tube level without a tube - although I'm surprised ReSharper knows this).

Please sign in to leave a comment.