[C#] EventHandlerDictionary CallUtilities NullReferenceException

Spent the last hour or two trying to figure out where the following exception was coming from:

[   8347953] Error invoking callback for event __cfx_nui:ACC2DE26A37B104FBCF4FD926EDBA1B5_Clicked: System.NullReferenceException: Object reference not set to an instance of an object.
[   8347953]   at CitizenFX.Core.CallUtilities.<GetPassArguments>g__ChangeType|0_0 (System.Object value, System.Type type) [0x00000] in C:\gl\builds\aaf2d114\0\cfx\fivem\code\client\clrcore\EventHandlerDictionary.cs:112 
[   8347968]   at CitizenFX.Core.CallUtilities.GetPassArguments (System.Reflection.MethodInfo method, System.Object[] args, System.String sourceString) [0x00066] in C:\gl\builds\aaf2d114\0\cfx\fivem\code\client\clrcore\EventHandlerDictionary.cs:159 
[   8347984]   at CitizenFX.Core.EventHandlerEntry+<Invoke>d__5.MoveNext () [0x00047] in C:\gl\builds\aaf2d114\0\cfx\fivem\code\client\clrcore\EventHandlerDictionary.cs:84 

Turns out the cause is here:

Specifically in my case, I was sending NUI updates (from JS -> C#) with a “null” message, which should be completely acceptable (for instance, ‘Click’ events that are tied to the event name, not some arguments passed back to the C# client). Simply checking for null, and passing it as null should fix this issue all together.

We shouldn’t be forced to make sure no argument is non-null (which currently is the case, and severely hinders some events)

1 Like