I receive a System.NullReferenceException: Object reference not set on an instance of an object when trying to add a value to a table that is running


I have a piece of code:

EDIT: The _penParams are initialized as the added line below.

    ProjectionParameters _penParams = new ProjectionParameters();

    [Given(@"Rate Rule List $raterule")]
    public void Rate_Rule_List(Int32 raterule)




It references an integer array defined as:

        private Collection<Int32> rateRuleIds;
    /// <summary>
    /// A collection of rate rule Ids the member has selected. This is only relevant for an AgeServiceOptions Rates Mode.
    /// </summary>
    public Collection<Int32> RateRuleIds
        get { return rateRuleIds; }

Two things have happened:

  1. The .Add method is not available to me when I try to compile, it was available in a previous instance, but has disappeared since I switched from working directly with the DLL to calling a web service to do my testing.
  2. If I try to access any part of the array, any of its properties, I get a "System.NullReferenceException : Object reference not set to an instance of an object" error.

Any thoughts would be greatly appreciated!

BTW: I am using NBehave to develop a simple syntax to allow non techie people specify end user conditions to be tested.

The reference to your collection is null, typically as a result of a failure to initialize the collection. The null reference exception means that you are trying to access a member on an instance that does not exist. (Is there a reason you don't initialize the collection in-line where you declare it?)

Based on other comments, I suspect that you're confused about the initialization. You state that you initialize this.rateRuleIds in ProjectionParameters(). Are you certain that ProjectionParameters() is being called before you ever do anything with rateRuleIds or RateRuleIds? If so, are you certain that the collection is not then later being set back to null?

I suggest, as a troubleshooting step, setting a breakpoint in ProjectionParameters() at the line you mention, this.rateRuleIds = new Collection<int>();, and one on the RateRuleIds.get property accessor. Then I suggest running the code to make sure that ProjectionParameters is actually executed before you ever get or use rateRuleIds. If it is executed, continue stepping through, verifying that the value of this.rateRuleIds is what you expect it to be every step of the way until you encounter your NullReferenceException.