Robert
Robert

Reputation: 5

My delegates event is returning null . Don't know what I am doing wrong

using System;
using System.Collections.Generic;
using System.Linq;
namespace practice
{
    class program
    {
        static void Main(string[] args)
        {
            Infoma infoma = new Infoma();
            infoma.footballers("Messi", 10);
            infoma.coach("Xavi Hernández", "Barcelona");
            Console.ReadLine();
        }

       
    }

    class Fifa 
    {
        private Infoma infoma;

        public Fifa()
        {
            infoma = new Infoma();
            infoma.Ievent += displayevent;
        }
        public void displayevent()
        {
            Console.WriteLine("This message was generated on " + DateTime.Now.ToShortTimeString());
        }
       
    }

    class Infoma
    {
        public delegate void Mevent();
        public event Mevent Ievent;
        public void footballers(string name, int no)
        {
            Console.WriteLine($"Player Name: {name}, Player NO: {no}");
            Ievent(); Console.WriteLine();
        }

        public void coach (string name , string club)
        {
            Console.WriteLine($"Coach Name : {name}, Club: {club}");
            Ievent();
        }
    }
}

I wrote this code (trying to get better using events and delegates) but when i run this code i get this error <System.NullReferenceException: 'Object reference not set to an instance of an object.' Ievent was null.> which is not suppose to be. Actually, I do not know what I am doing wrong. If anyone can help or advise why my event is returning null . Thanks y'all

Upvotes: 0

Views: 428

Answers (2)

Enigmativity
Enigmativity

Reputation: 117154

The only place that was attaching to your event was inside fifa, but you never instantiated that class, so your event delegate was null.

Here's code that works:

class program
{
    static void Main(string[] args)
    {
        Fifa fifa = new Fifa();
        fifa.Infoma.footballers("Messi", 10);
        fifa.Infoma.coach("Xavi Hernández", "Barcelona");
        Console.ReadLine();
    }
}

class Fifa
{
    public readonly Infoma Infoma;

    public Fifa()
    {
        Infoma = new Infoma();
        Infoma.Ievent += displayevent;
    }
    public void displayevent()
    {
        Console.WriteLine("This message was generated on " + DateTime.Now.ToShortTimeString());
    }

}

class Infoma
{
    public delegate void Mevent();
    public event Mevent Ievent;
    public void footballers(string name, int no)
    {
        Console.WriteLine($"Player Name: {name}, Player NO: {no}");
        Ievent();
        Console.WriteLine();
    }

    public void coach(string name, string club)
    {
        Console.WriteLine($"Coach Name : {name}, Club: {club}");
        Ievent();
    }
}

Here's your code using more standard naming:

class Program
{
    static void Main(string[] args)
    {
        Fifa fifa = new Fifa();
        fifa.Infoma.Footballers("Messi", 10);
        fifa.Infoma.Coach("Xavi Hernández", "Barcelona");
        Console.ReadLine();
    }
}

class Fifa
{
    public readonly Infoma Infoma;

    public Fifa()
    {
        Infoma = new Infoma();
        Infoma.Message += DisplayMessage;
    }
    public void DisplayMessage()
    {
        Console.WriteLine("This message was generated on " + DateTime.Now.ToShortTimeString());
    }
}

class Infoma
{
    public delegate void MessageHandler();
    public event MessageHandler Message;
    public void Footballers(string name, int no)
    {
        Console.WriteLine($"Player Name: {name}, Player NO: {no}");
        Message();
        Console.WriteLine();
    }

    public void Coach(string name, string club)
    {
        Console.WriteLine($"Coach Name : {name}, Club: {club}");
        Message();
    }
}

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54457

EDIT:

My answer below covers how you would prevent the exception you're seeing being thrown, but it doesn't cover what I think is your immediate problem, which is why there's no event handler registered in the first place. To explain that, it is the Fifa class that handles the event but you never use that class in your Main method, so no event handler is ever registered. If you had put a breakpoint on the line that registers the event handler, as you should have done, you'd have seen that it was never executed.

ORIGINAL:

I suggest that you read this for more information in defining and raising your own events. In short, you should start by declaring your event as type EventHandler or, if it needs to provide data, EventHandler<TEventArgs>. There's no good reason to declare your own delegate type.

public event EventHandler SomethingHappened;

As with EVERYTHING, you should use a descriptive name for your event. Ievent is a terrible name. The name should specify the reason that the event is being raised.

Next, you should declare a method to raise the event. That method should be protected and virtual so it cannot be executed from outside but derived classes can raise and augment the event. Thos method should also test whether the event itself is null, which is what you're not doing. the event will be null if no listeners have registered to handle the event:

protected virtual void OnSomethingHappened(EventArgs e)
{
    if (SomethingHappened != null)
    {
        SomethingHappened(this, e);
    }
}

The arguments you pass when you raise the event are what populate the sender and e parameters in the event handler. Note that the method is named after the event with the prefix "On".

You then raise the event by calling that method:

OnSomethingHappened(EventArgs.Empty);

These days, you can use a null propagation operator instead of an explicit null check. If your event is type EventHandler, there's also not really a need to pass in EventArgs.Empty every time you call the method. That can be built into the method body:

protected virtual void OnSomethingHappened()
{
    SomethingHappened?.Invoke(this, EventArgs.Empty);
}

Even if you ignore all the other advice here, which you obviously should not, it is that explicit or implicit null check that you are missing and need to incorporate into your code to avoid that NullReferenceException when no event handler is registered.

Upvotes: 1

Related Questions