C
C#2y ago
Asher

❔ Using Event Handler null exception

Definition (in EventHandlerClass) public EventHandler<ActionMessage> OnAction; Usage: EventHandlerClass.OnAction += ActionUsage; this gives me a null exception on the Usage line, is there something I'm doing wrong here? (this is before invoking, or doing anything else, however, I do think it has something to do with OnAction not being initialized, but I couldn't find any examples where it is initialized. The handler signature and event args match. Is this correct usage? or do I have something else wrong with my code?
17 Replies
TheBoxyBear
TheBoxyBear2y ago
It needs to be defined as an event public event EventHandler<ActionMessage> OnAction
Asher
AsherOP2y ago
same thing still crashes right on that line and says something is null does the ActionUsage definition have to come before the usage line? this is the signature:
private void ActionUsage(object? sender, ActionMessage message)
private void ActionUsage(object? sender, ActionMessage message)
I'd find it weird that intellisense wouldn't catch that though
TheBoxyBear
TheBoxyBear2y ago
The placement of methods in classes doesn't matter Unless it's a local function but it seems to be a member since it's private
Asher
AsherOP2y ago
its a member yeah
TheBoxyBear
TheBoxyBear2y ago
Crashes on the +=?
Asher
AsherOP2y ago
yeah I have logs there so I can see exactly when it crashes
TheBoxyBear
TheBoxyBear2y ago
All I can think of is the missing event keyword or the instance of the class with the event being null
Asher
AsherOP2y ago
weird, I can see that the instance that I use (EventHandlerClass) is not null either
TheBoxyBear
TheBoxyBear2y ago
Should maybe rename the class or delegate to avoid confusion for a start The class* Also EventHandlerClass.OnAction += ActionUsage; that only works if the event is static but it shouldn't compile in that case
Asher
AsherOP2y ago
i misnamed it its not the class its the instance long story on that one, doesn't matter in this case i can't print either of them either to check whats exactly going on since ones an event and ones a function
TheBoxyBear
TheBoxyBear2y ago
That seems impossible unless there's something else up with the code
Asher
AsherOP2y ago
potentially but I'm working on a big project and its really hard to debug this
TheBoxyBear
TheBoxyBear2y ago
You can try printing values from the instance before subscribing to its event
Asher
AsherOP2y ago
true, lemme do that real quick yeah seems like something is wrong with the instance itself, also very weird as in the other class the instance is working completely fine, and this instance is the same one but thanks for the help :)
jast.313
jast.3132y ago
is 'EventHandlerClass' a property? maybe you're doing something that throws the exception in the property getter?
TheBoxyBear
TheBoxyBear2y ago
EventHandlerClass a class containing the OnAction event
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?