3

I'am having a very annoying problem in my code, when I try to send a string from Form B to form a. I get the error message:

Object reference not set to an instance of an object.

I'am familiar with this error and normally I know how to solve this problem, but this one is different.

I need to send a Clockname from one form to the main form, I'am trying to achieve this using the code below:

delegate void ClockClocknameReceivedEventHandler(object sender, Clock.ClocknameReceivedEventArgs e);

    internal class ClocknameReceivedEventArgs : EventArgs
    {
        string _clockname;

        public string Clockname
        {
            get { return _clockname; }
        }

        public ClocknameReceivedEventArgs(string clockname)
        {
            _clockname = clockname;
        }
    }

    // An event that clients can use to be notified whenever the
    // elements of the list change.
    public event ClockClocknameReceivedEventHandler ClocknameReceived;

    // Invoke the Changed event; called whenever list changes
    protected void OnClocknameReceived(Clock.ClocknameReceivedEventArgs e)
    {
        ClocknameReceived(this, e);
    }

And the following code gets fired when pressing a button, the form will close after that:

OnClocknameReceived(new Clock.ClocknameReceivedEventArgs(ClockName));

The error(Object reference not set to an instance of an object.) I receive occurs at

ClocknameReceived(this, e);

I'am using the exact same code, from another class to the main form to send a byte array which works fine, but this one give me that error.

Anyone any ideas?

Thanks in advance!

3 Answers 3

4

The delegate can be null. Invoke it only if it's not null:

protected void OnClocknameReceived(Clock.ClocknameReceivedEventArgs e)
{
    ClockClocknameReceivedEventHandler handler = ClocknameReceived;
    if (handler != null)
    {
        handler(this, e);
    }
}

The delegate is null when you haven't subscribed an event handler to the event yet. Subscribe an event handler:

formB.ClocknameReceived += FormB_ClocknameReceived;

with

void FormB_ClocknameReceived(object sender, Clock.ClocknameReceivedEventArgs e)
{
    MessageBox.Show(e.Clockname);
}
Sign up to request clarification or add additional context in comments.

7 Comments

I already tried that, the problem is that it won't execute ever, how can I get ClocknameReceived filled so it won't get null?
Have you subscribed an event handler to the event? See: How to: Subscribe to and Unsubscribe from Events (C# Programming Guide)
Yes I did that on the main form, and that one isn't giving me any problems
Have you checked if OnClocknameReceived is called before the event handler is subscribed?
@Mobstaa I believe your problem is what dtb describes. You call OnClocknameReceived before attaching the event handler. That would explain both behaviours. The null exception and doing nothing after checking for null.
|
1

Your not checking whether the ClocknameReceived event has been assigned (i.e. has any subscribers). Typical event handling code generally looks like:

var handler = ClocknameReceived;
if (handler != null)
{
    handler(this, e);
}

This type of approach also mitigates (to an extent) race conditions with your event handler as it could be unassigned by the time you go to trigger it.

Looking at your code you could definitely tidy this up a bit. For one, your EventArgs class could be re-written with less code:

internal class ClocknameEventArgs : EventArgs
{
    public ClockNameEventArgs(string name)
    {
        Name = name;
    }

    public string Name { get; private set; }
}

Then in your form, there's no need for a delegate if you have a particular type of EventArgs, your event can be declared as:

public event EventHandler<Clock.ClocknameEventArgs> ClocknameReceived;

You then hook up to this event somewhere (maybe in the FormCreate event?):

ClocknameReceived += (sender, args) {
    FormB.PassName(args.Name);
};

Comments

0

You have to check if the event has a delegate or not

if( ClocknameReceived != null)
    ClocknameReceived(this, e);

2 Comments

The problem is, how do I fill it with something not equal to null, this doesn' t solve the problem
@Mobstaa If you attach properly to the event handler the delegate won't be null. Something like formVariable.ClocknameReceived += new ClockClocknameReceivedEventHandler(handler); and private void handler(object sender, Clock.ClocknameReceivedEventArgs e)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.