views:

278

answers:

5

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:

  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.

A: 

Do you ever initialize your collection like so :

rateRuleIds = new Collection<Int32>();
Dynami Le Savard
A: 

looks like

 private Collection<Int32> rateRuleIds;

is not initialised to a new Collection<Int32>() anywhere...

EDIT:

so you say you have initialised the collection. So is _penParams actually initialized?

Why can you not debug the code and see what the issue is?

Put a break point on the code where the collection gets initialized and make sure it is being called. Put a break point on the line that falls over and inspect the variable to see which one is null.

Sam Holder
public ProjectionParameters() { this.rateRuleIds = new Collection<int>(); }Its initialized in the larger collection of parameters.
Ivor
+1  A: 

When do you actually initialize your Array:

rateRuleIds = new Collection<Int32>();

EDIT:

Since you have stated that you are in fact initializing the variable, then I will have to trust you. However, I am not really sure what this line is:

_penParams.RateRuleIds.Initialize();

Is Initialize() some kind of extension method? Because it is not part of the Collection class.

Josh
+4  A: 
private Collection<Int32> rateRuleIds;

you need to initialize rateRuleIds as it is only declared yet.

Collection<Int32> rateRuleIds = new Collection<int>();

Declaration of an object tells compiler this object exist, this is the specification and get ready to handle it. Initialization, on the other hand allocates the memory for the object.

Asad Butt
I think Ivor has already mentioned that he is in fact initializing the variable elsewhere...
Josh
If the pasted code is the initialization code, it is not (and in fact, cannot) initialize the collection because there is no setter on the `RateRuleIds` property.
Greg D
The SO equivalent to the "pic or didn't happen" rule is "paste the code or you didn't write it".
xcud
+1  A: 

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.

Greg D
I had to pick one answer and Gregs proved to be most constructive in helping me disgnose the problem. The problem was twofold:1. The RateRuleIds was readonly, and the developer has since changed it to allow me access.2. I had to initialize the RateRuleIds array correctly by adding this line at the start of my code: penParams.RateRuleIds = new Int32[10]. The size didn't matter as there is a finite number of rate ids that can be provided.Thanks for your help, it is much appreciated. Ivor
Ivor
@Ivor: For what it's worth, the developer who owns the RateRuleIds property should generally be the person responsible for ensuring that it's properly initialized before being exposed on their public interface. Glad I could help. :)
Greg D