我必须忘记,因为我的单元测试失败,因为以下代码抛出了null ref异常:
int pid = 0;
if (parentCategory != null)
{
Console.WriteLine(parentCategory.Id);
pid = parentCategory.Id;
}
Run Code Online (Sandbox Code Playgroud)
投掷它的线是:
pid = parentCategory.Id;
Run Code Online (Sandbox Code Playgroud)
console.writeline只是在NUnit GUI中调试,但输出有效的int.
编辑:它是单线程的,因此无法从其他某个线程分配为null,而Console.WriteLine成功打印出该值的事实表明它不应该抛出.
编辑: Category类的相关片段:
public class Category
{
private readonly int id;
public Category(Category parent, int id)
{
Parent = parent;
parent.PerformIfNotNull(() => parent.subcategories.AddIfNew(this));
Name = string.Empty;
this.id = id;
}
public int Id
{
get { return id; }
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,如果有人想查看完整的代码,请访问http://code.google.com/p/chefbook/source/checkout上的Google Code
我想我会尝试重新启动计算机...我已经看到通过重启修复了很奇怪的事情.重启后会更新.
更新:神秘解决了.看起来NUnit将错误行显示为最后成功执行的语句...将复制/粘贴测试复制到新控制台应用程序并在VS中运行显示它是包含空引用的if语句块(未显示)之后的行.感谢大家的所有想法.给每个回答的人+1.
基于到目前为止的所有信息,我认为"抛出的线条"是错误的(是什么让你认为),或者你的'源'可能与你的构建组件不同步.
这看起来是不可能的,因此一些"理所当然的假设"是错误的,并且可能假设"您正在查看的源代码与您正在调试的过程相匹配".