Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 700279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:28:47+00:00 2026-05-14T03:28:47+00:00

I have a StatusChanged event that is raised by my object when its status

  • 0

I have a StatusChanged event that is raised by my object when its status changes – however, the application needs to carry out additional actions based on what the new status is.

e.g If the new status is Disconnected, then it must update the status bar text and send an email notification.

So, I wanted to create an Enum with the possible statuses (Connected, Disconnected, ReceivingData, SendingData etc.) and have that sent with the EventArgs parameter of the event when it is raised (see below)

Define the object:

class ModemComm
{
    public event CommanderEventHandler ModemCommEvent;
    public delegate void CommanderEventHandler(object source, ModemCommEventArgs e);

    public void Connect()
    {
        ModemCommEvent(this, new ModemCommEventArgs ModemCommEventArgs.eModemCommEvent.Connected));
    }
}

Define the new EventArgs parameter:

public class ModemCommEventArgs : EventArgs{
    public enum eModemCommEvent
    {
        Idle,
        Connected,
        Disconnected,
        SendingData,
        ReceivingData
    }

    public eModemCommEvent eventType { get; set; }
    public string eventMessage { get; set; }

    public ModemCommEventArgs(eModemCommEvent eventType, string eventMessage)
    {
        this.eventMessage = eventMessage;
        this.eventType = eventType;
    }
}

I then create a handler for the event in the application:

ModemComm comm = new ModemComm();
comm.ModemCommEvent += OnModemCommEvent;

and

private void OnModemCommEvent(object source, ModemCommEventArgs e)
{
}

The problem is, I get a ‘Object reference not set to an instance of an object’ error when the object attempts to raise the event. Hoping someone can explain in n00b terms why and how to fix it 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:28:47+00:00Added an answer on May 14, 2026 at 3:28 am

    Events are null when no clients are subscribed to them, so an attempt to invoke an event that has no subscribers will fail with a NullReferenceException.

    Some common techniques to avoid this:

    1) Check for null in a thread-safe manner (from the event raiser’s perspective; there is still a race condition on the client, however it’s their responsibility to be handle that)

    var handler = this.ModemCommEvent;
    if( handler != null ) {
        handler(this, new ModemCommEventArgs( ModemCommEventArgs.eModemCommEvent.Connected ));
    }
    

    The above code is the more complex version of this:

    if( this.ModemCommEvent != null ) {
        this.ModemCommEvent(this, new ModemCommEventArgs(ModemCommEventArgs.eModemCommEvent.Connected));
    }
    

    The first one (that creates a local variable) is safer from the event raiser’s perspective because the local variable will either be null or it won’t and nothing will change that. In the second one, however, a client running on a separate thread could unsubscribe from the event between the time your check for null is done and you raise the event. In that case, you’d end up with a NullReferenceException again. If neither you nor your code’s clients are executing on multiple threads (no BackgroundWorker, Thread object, asynchronous invoke, etc), then the safer check is redundant. If you’re not sure, however, it’s a good practice to get into. That, or do #2.

    2) Default your event to an empty value

    public event CommanderEventHandler ModemCommEvent = delegate { };
    

    This side-steps the issue completely by always having at least one subscriber. The “delegate {}” syntax creates an anonymous method that does nothing that is the “default subscriber” to the event. No matter how many clients subscribe or unsubscribe from your event, this anonymous method will always be there, preventing your event from being null.

    —

    This has been discussed ad nauseum all over the internet. Here is one such example:

    C# Events and Thread Safety

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that has been running fine since its launch over a
HI All, I have submitted an ipad app in the app store.Its status changed
The situation I have an ASP.NET Webform that contains some status information about a
have written this little class, which generates a UUID every time an object of
Have deployed numerous report parts which reference the same view however one of them
I have a JSlider that sets the speed of my metronome, from 40 -
I have a query SELECT assetid, type_code, version, name, short_name, status, languages, charset, force_secure,
I have a database with 2 tables that I am working with (SugarCRM). I
I have this property: public SubjectStatus Status { get { return status; } set
i have a list of buffers that i display into a telerik radchart. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.