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)
{
_penParams.RateRuleIds.Initialize();
_penParams.RateRuleIds.Add(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:
- 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.
- 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.rateRuleIdsinProjectionParameters(). Are you certain thatProjectionParameters()is being called before you ever do anything withrateRuleIdsorRateRuleIds? 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 theRateRuleIds.getproperty accessor. Then I suggest running the code to make sure thatProjectionParametersis actually executed before you ever get or userateRuleIds. If it is executed, continue stepping through, verifying that the value ofthis.rateRuleIdsis what you expect it to be every step of the way until you encounter yourNullReferenceException.