Why null reference exception in SetMolePublicInstance?

Posted by OldGrantonian on Stack Overflow See other posts from Stack Overflow or by OldGrantonian
Published on 2012-04-01T17:41:19Z Indexed on 2012/04/06 11:29 UTC
Read the original article Hit count: 189

Filed under:

I get a "null reference" exception in the following line:

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

The program builds and compiles correctly. There are no complaints about any of the parameters to the method.

Here's the specification of SetMolePublicInstance, from the object browser:

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

Here are the parameter values for "Locals":

+ 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[]

I know that TestString() takes no parameters and returns string, so as a starter to try to get things working, I specified "null" for the final parameter to SetMolePublicInstance. As already mentioned, this compiles OK.

Here's the stack trace:

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 . . .

To avoid the null parameter, I changed the final "null" to "parameterTypes" as in the following line:

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

I then tried each of the following (before the line):

int[] parameterTypes = null;          // if this is null, I don't think the type will matter
int[] parameterTypes = new int[0];
object[] parameterTypes = new object[0];     // this would allow for various parameter types

All three attempts produce a red squiggly line under the entire line for SetMolePublicInstance

Mouseover showed the following message:

The best overloaded method match for 'Microsoft.Moles.Framework.Moles.MoleRuntime.SetMolePublicInstance(System.Delegate, System.Type, object, string, params System.Type[])' has some invalid arguments.

I'm assuming that the first four arguments are OK, and that the problem is with the params array.

© Stack Overflow or respective owner

Related posts about Moles