简体   繁体   中英

I am getting a System.NullReferenceException : Object reference not set to an instance of an object when trying to add a value to an array at runtime

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.

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.

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.

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 .

您是否曾经像这样初始化过您的收藏集:

rateRuleIds = new Collection<Int32>();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM