Linked Questions

Popular Questions

Delegate Eventhandler is null

Asked by At

okay im new to delegates so not sure how to fix this, nor do i know how to describe it...

1, here is my code

i have tried changing the order of code, setting == null (which obviously didnt work), i have googled this issue and cant find any help at all...

namespace Thing
{
    public class OtherThing
    {
        public delegate void TextChangedEventHandler(object source, EventArgs args);

    public event TextChangedEventHandler TextHasChanged;

    public String BDIFormcsLabelText;

    public String TEXT
    {
        get
        {
            return BDIFormcsLabelText;
        }
        set
        {
            BDIFormcsLabelText = value;
            OnTextHasChanged();

        }
    }

    protected virtual void OnTextHasChanged()
    {
        if (TextHasChanged != null) // <---- THIS IS NULL
        {
            TextHasChanged(this, EventArgs.Empty);
        }
    }
 }
}

Im trying to get it to detect a change in the variable BDIFormcsLabelText and then fire an event. the detect in change works fine, its the fireing the vent that isnt... it calls the "OnTextHasChanged" event handler but what is labled null in the code is null and i cant seem to figure it out. it throws an error saying

"System.NullReferenceException: 'Object reference not set to an instance of an object.'"

I dont know enough about this to explain further. I just know thats whats stopping my subscribers to the event from executing there code. if anyone can help me fix this then THANK YOU

The Subscriber to the event's code looks like this

    Thing thing = new Thing();

    public void BDIFormcs_Load(object sender, EventArgs e)
    {
        thing.BDIFormcsLabelText = label1.Text;
        thing.TextHasChanged += OnTextHasChanged;
    }

    public void OnTextHasChanged(object source, EventArgs e)
    {
        label1.Text = thing.BDIFormcsLabelText;
    }

Related Questions