❔ 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
It needs to be defined as an event
public event EventHandler<ActionMessage> OnAction
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:
I'd find it weird that intellisense wouldn't catch that thoughThe 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
its a member yeah
Crashes on the +=?
yeah
I have logs there so I can see exactly when it crashes
All I can think of is the missing
event
keyword or the instance of the class with the event being nullweird, I can see that the instance that I use (EventHandlerClass) is not null either
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 casei 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
That seems impossible unless there's something else up with the code
potentially but I'm working on a big project and its really hard to debug this
You can try printing values from the instance before subscribing to its event
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 :)
is 'EventHandlerClass' a property? maybe you're doing something that throws the exception in the property getter?
EventHandlerClass a class containing the OnAction event
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.