我收到 System.NullReferenceException :尝试在运行时向数组添加值时未将对象引用设置为对象的实例
我有一段代码:
编辑: _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; }
}
发生了两件事:
- 当我尝试编译时,.Add 方法对我不可用,它在以前的实例中可用,但自从我从直接使用 DLL 切换到调用后就消失了用于进行测试的网络服务。
- 如果我尝试访问数组的任何部分及其任何属性,则会收到“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:
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要
初始化
rateRuleIds,因为它只是声明
。you need to
initialize
rateRuleIds as it is onlydeclared
yet.对集合的引用为空,通常是由于初始化集合失败造成的。空引用异常意味着您正在尝试访问不存在的实例上的成员。 (您没有在声明集合的地方内嵌初始化集合吗?)
根据其他评论,我怀疑您对初始化感到困惑。您声明您在
ProjectionParameters()
中初始化this.rateRuleIds
。您确定在使用rateRuleIds
或RateRuleIds
执行任何操作之前会调用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
inProjectionParameters()
. Are you certain thatProjectionParameters()
is being called before you ever do anything withrateRuleIds
orRateRuleIds
? 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.get
property accessor. Then I suggest running the code to make sure thatProjectionParameters
is actually executed before you ever get or userateRuleIds
. If it is executed, continue stepping through, verifying that the value ofthis.rateRuleIds
is what you expect it to be every step of the way until you encounter yourNullReferenceException
.你什么时候真正初始化你的数组:
编辑:
既然你已经声明你实际上正在初始化变量,那么我就必须相信你。但是,我不太确定这一行是什么:
Is
Initialize()
some kind of extension method?因为它不是 Collection 类的一部分。When do you actually initialize your Array:
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:
Is
Initialize()
some kind of extension method? Because it is not part of the Collection class.你有没有像这样初始化你的集合:
Do you ever initialize your collection like so :
看起来
没有在任何地方初始化为
new Collection()
...编辑:
所以你说你已经初始化了集合。那么_penParams实际上已经初始化了吗?
为什么你不能调试代码并看看问题出在哪里?
在集合初始化的代码上放置一个断点,并确保它被调用。在掉落的行上放置一个断点并检查变量以查看哪一个为空。
looks like
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.