0

Here is the faulty code section :

JoinAccueilEventArgs jaea = new JoinAccueilEventArgs(this._user);
if (this._user==null)
{
    Console.WriteLine("user...");
}

if (this == null)
{
    Console.WriteLine("this...");
}

if (jaea == null)
{
    Console.WriteLine("jaea...");
}
Console.ReadLine();
JoinAccueilEvent(this, jaea);

I'm getting a NullReferenceException on the last line (JoinAccueilEvent(...) But nothing in console..

So what is null here ??

internal static event JoinAccueilEventHandler JoinAccueilEvent;
internal delegate void JoinAccueilEventHandler(object sender, JoinAccueilEventArgs e);

private void JoinAccueilHandler(object sender, JoinAccueilEventArgs e)
{
    _callback.UserJoinAccueil(e.User);
}
John Saunders
  • 157,405
  • 24
  • 229
  • 388
Flozza
  • 131
  • 2
  • 10
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 21 '13 at 00:26
  • FYI, one "raises" an event, one does not "throw" an event. One "throws" exceptions. – John Saunders Dec 21 '13 at 00:28

1 Answers1

4

Most probably JoinAccueilEvent is null, you should check it in typical way when using events:

if (JoinAccueilEvent != null)
   JoinAccueilEvent(this, jaea)

The problems comes from the fact that if nobody subscribes the event, it is null. You can overcome this by adding do-nothing handler by default:

internal static event JoinAccueilEventHandler JoinAccueilEvent = delegate {};

In this case, checking for null is not necessary as there is always at least one event subscriber.

Konrad Kokosa
  • 15,790
  • 2
  • 33
  • 54
  • oh, yes you got it .. Thanks you. Is there any reason ? I'm little confused about that. what i can have missed here ? EDIT : No subscriber, yep.. Thanks you ! – Flozza Dec 21 '13 at 00:12