gpt4 book ai didi

c# - 我收到 System.NullReferenceException : Object reference not set to an instance of an object when trying to add a value to an array at runtime

转载 作者:太空宇宙 更新时间:2023-11-03 19:34:58 25 4
gpt4 key购买 nike

我有一段代码:

编辑:_penParams 初始化为下面添加的行。

    ProjectionParameters _penParams = new ProjectionParameters();

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

_penParams.RateRuleIds.Initialize();

_penParams.RateRuleIds.Add(raterule);

}

它引用一个整数数组定义为:

        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; }
}

发生了两件事:

  1. .Add 方法在我尝试编译时不可用,它在之前的实例中可用,但自从我从直接使用 DLL 切换到调用 Web 服务来进行测试后就消失了。<
  2. 如果我尝试访问数组的任何部分及其任何属性,我会收到“System.NullReferenceException:对象引用未设置为对象的实例”错误。

如有任何想法,我们将不胜感激!

顺便说一句:我正在使用 NBehave 开发一个简单的语法,以允许非技术人员指定要测试的最终用户条件。

最佳答案

private Collection<Int32> rateRuleIds;

您需要初始化 rateRuleIds,因为它只是声明

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.

关于c# - 我收到 System.NullReferenceException : Object reference not set to an instance of an object when trying to add a value to an array at runtime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2318698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com