我收到 System.NullReferenceException :尝试在运行时向数组添加值时未将对象引用设置为对象的实例

发布于 2024-08-23 08:25:02 字数 973 浏览 1 评论 0原文

我有一段代码:

编辑: _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 切换到调用后就消失了用于进行测试的网络服务。
  2. 如果我尝试访问数组的任何部分及其任何属性,则会收到“System.NullReferenceException:未将对象引用设置为对象实例”错误。

任何想法将不胜感激!

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

漫雪独思 2024-08-30 08:25:02
private Collection<Int32> rateRuleIds;

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

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

对象的声明告诉编译器该对象存在,这是
规格并准备好
处理它。 初始化,在
另一只手分配内存
对象。

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.

撩动你心 2024-08-30 08:25:02

对集合的引用为空,通常是由于初始化集合失败造成的。空引用异常意味着您正在尝试访问不存在的实例上的成员。 (您没有在声明集合的地方内嵌初始化集合吗?)

根据其他评论,我怀疑您对初始化感到困惑。您声明您在 ProjectionParameters() 中初始化 this.rateRuleIds。您确定在使用 rateRuleIdsRateRuleIds 执行任何操作之前会调用 ProjectionParameters() 吗?如果是这样,您确定该集合稍后不会被设置回 null 吗?

我建议,作为故障排除步骤,在 ProjectionParameters() 中您提到的行 this.rateRuleIds = new Collection(); 处设置一个断点,以及一个在 RateRuleIds.get 属性访问器上。然后,我建议运行代码以确保在获取或使用 rateRuleIds 之前确实执行 ProjectionParameters。如果已执行,请继续单步执行,验证 this.rateRuleIds 的值是否是您期望的每一步,直到遇到 NullReferenceException

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.

静谧 2024-08-30 08:25:02

你什么时候真正初始化你的数组:

rateRuleIds = new Collection<Int32>();

编辑:

既然你已经声明你实际上正在初始化变量,那么我就必须相信你。但是,我不太确定这一行是什么:

_penParams.RateRuleIds.Initialize();

Is Initialize() some kind of extension method?因为它不是 Collection 类的一部分。

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.

贵在坚持 2024-08-30 08:25:02

你有没有像这样初始化你的集合:

rateRuleIds = new Collection<Int32>();

Do you ever initialize your collection like so :

rateRuleIds = new Collection<Int32>();
眼泪淡了忧伤 2024-08-30 08:25:02

看起来

 private Collection<Int32> rateRuleIds;

没有在任何地方初始化为 new Collection()...

编辑:

所以你说你已经初始化了集合。那么_penParams实际上已经初始化了吗?

为什么你不能调试代码并看看问题出在哪里?

在集合初始化的代码上放置一个断点,并确保它被调用。在掉落的行上放置一个断点并检查变量以查看哪一个为空。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文