-1

I get the message "Object reference not set to an instance of an object." on this line of code: LaneControllerLane(this, EventArgs.Empty);

This is the whole program:

MainForm

public static event EventHandler LaneControllerDevice;
public static event EventHandler LaneControllerLane;
internal void LaneMessage(string message)
    {

        CommonEntitiesForTollApps.LANE = message.Split('|')[0];
        CommonEntitiesForTollApps.DEVICE = message.Split('|')[1];

        pnlForControl.BeginInvoke(new Action(() =>
        {
            LaneControllerLane(this, EventArgs.Empty);
        }));
    }

ControlForm

private void ctrlLane_Load(object sender, EventArgs e)
        {

            MainForm.LaneControllerDevice += new EventHandler(CheckSignal);
            MainForm.LaneControllerLane += new EventHandler(CheckLaneOperation);

        }

private void CheckLaneOperation(object sender, EventArgs e)
    {
        if(CommonEntitiesForTollApps.LANE == this.LaneName)
        {
            switch (DEVICE)
            {
                case "Scanner":
                    lblScanner.Text = "1";
                    break;
                case "Printer":
                    lblPrinter.Text = "1";
                    break;
                case "RFID":
                    lblTransactionType.Text = CommonEntitiesForTollApps.TRANSACTION_TYPE;
                    break;
            }
        }
        else
        {
            return;
        }
    }

Can you please point out what I am doing wrong? Thank you.

Ibanez1408
  • 3,275
  • 8
  • 31
  • 75
  • Before raising an event you should check it anyone subscribed checking for nulls. check the duplicated link or Mighty's answer – Cleptus Jul 20 '17 at 07:05

1 Answers1

1

Replace

LaneControllerLane(this, EventArgs.Empty);

with

LaneControllerLane?.Invoke(this, EventArgs.Empty);

This is how you fire an event. The ? is the short form of a null check: null conditional operators

If you are using older frameworks you have to use

if(LaneControllerLane != null)
{
    LaneControllerLane.Invoke(this, EventArgs.Empty);
}

instead.

Mighty Badaboom
  • 5,609
  • 5
  • 27
  • 47