为什么SetMolePublicInstance中的null引用异常?

时间:2012-04-01 17:41:20

标签: moles

我在以下行中得到“null reference”异常:

MoleRuntime.SetMolePublicInstance(stub, receiverType, objReceiver, name, null);

程序正确构建和编译。没有关于该方法的任何参数的抱怨。

以下是来自对象浏览器的SetMolePublicInstance规范:

SetMolePublicInstance(System.Delegate _stub, System.Type receiverType, object _receiver, string name, params System.Type[] parameterTypes)

以下是“当地人”的参数值:

+ stub {Method = {System.String <StaticMethodUnitTestWithDeq>b__0()}} System.Func<string>
+ receiverType {Name = "OrigValue" FullName = "OrigValueP.OrigValue"} System.Type {System.RuntimeType}
objReceiver {OrigValueP.OrigValue} object {OrigValueP.OrigValue}
name "TestString" string
parameterTypes null object[]

我知道TestString()不接受任何参数并返回字符串,因此作为尝试使事情正常工作的启动器,我为SetMolePublicInstance的最终参数指定了“null”。如前所述,这可以编译好。

这是堆栈跟踪:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at     Microsoft.ExtendedReflection.Collections.Indexable.ConvertAllToArray[TInput,TOutput](TInput[] array, Converter`2 converter)
   at Microsoft.Moles.Framework.Moles.MoleRuntime.SetMole(Delegate _stub, Type receiverType, Object _receiver, String name, MoleBindingFlags flags, Type[] parameterTypes)
   at Microsoft.Moles.Framework.Moles.MoleRuntime.SetMolePublicInstance(Delegate _stub, Type receiverType, Object _receiver, String name, Type[] parameterTypes)
   at DeqP.Deq.Replace[T](Func`1 stub, Type receiverType, Object objReceiver, String name) in C:\0VisProjects\DecP_04\DecP\DeqC.cs:line 38
   at DeqPTest.DecCTest.StaticMethodUnitTestWithDeq() in C:\0VisProjects\DecP_04\DecPTest\DeqCTest.cs:line 28
   at Starter.Start.Main(String[] args) in C:\0VisProjects\DecP_04\Starter\Starter.cs:line 14
Press any key to continue . . .

为了避免使用null参数,我将最后的“null”更改为“parameterTypes”,如下所示:

MoleRuntime.SetMolePublicInstance(stub, receiverType, objReceiver, name, parameterTypes);

我插入了一行:

Type [] parameterTypes = new Type[0];

使用moles.runner.exe可以产生所需的结果:)

1 个答案:

答案 0 :(得分:0)

编译器无法检查NullReferenceException;因为,它是一个运行时异常。此异常表示传递给参数的对象之一为null,当期望它指向实例时。

在测试项目的该行上放置一个断点。在调试模式下运行测试将在该行中断,允许您查看哪些参数为空。

相关问题