Home All Groups Group Topic Archive Search About

Raising event from class results in NullReferenceException

Author
9 Feb 2005 6:27 AM
Neil Macdonald [Intergraph]

OK..

I have a class that runs a process that basically does some connection
checking; I have a delegate setup to handle the status update event which
will allow the client to give some kind of indication to the GUI.

I get a NullReferenceException when I try to raise the event. Below is the
class source...

-------------START CODE-------------------
using System;
using System.Threading;

namespace SomeClass.ConnectionChecker
{

    public class ConnectionManager : IDisposable
    {
        private bool _continuallyMonitorConnectionStatus = false;
        private ConnectionStatus _currentConnectionStatus =
ConnectionStatus.UnknownStatus;

        public ConnectionManager()
        {                       
            try
            {
                _currentConnectionStatus=_getCurrentConnectionStatus();               
                ThreadStart threadStartMethod = new ThreadStart(_monitorNetworkStatus);
                Thread threadConnectionMonitor = new Thread(threadStartMethod);
                _continuallyMonitorConnectionStatus=true;
                threadConnectionMonitor.Start();
            }
            catch(Exception operationException)
            {
                _continuallyMonitorConnectionStatus=false;
                throw new
ApplicationException("CommunicationsManager.CommunicationsManager(): " +
operationException.Message,operationException);
            }
        }


        public delegate void ConnectionStateChanged(object sender,
ConnectionStatusEventArgs e);
        public event ConnectionStateChanged OnConnectionStateChanged;
        public delegate void ConnectionStateUpdate(object sender,
ConnectionStatusEventArgs e);
        public event ConnectionStateUpdate OnConnectionStateUpdate;

        private ConnectionStatus _getCurrentConnectionStatus()
        {
            ConnectionStatus currentConnectionStatus = ConnectionStatus.UnknownStatus;
            try
            {
                //get the current status - raise status update event
                //OnConnectionStateUpdate(this,new
ConnectionStatusEventArgs(currentConnectionStatus));
----ERROR OCCURS ON THIS LINE


                OnConnectionStateUpdate(this,new
ConnectionStatusEventArgs(ConnectionStatus.UnknownStatus));
                //if that status differs from the current status raise change event
                if(currentConnectionStatus != _currentConnectionStatus)
                {
                    OnConnectionStateChanged(this,new
ConnectionStatusEventArgs(currentConnectionStatus));
                }
                _currentConnectionStatus=currentConnectionStatus;
                return ConnectionStatus.UnknownStatus;
            }
            catch(Exception privateMethodException)
            {
                _continuallyMonitorConnectionStatus=false;
                throw new
ApplicationException("ConnectionManager._setCurrentNetworkStatus: " +
privateMethodException.Message,privateMethodException);
            }
        }
        private void _monitorNetworkStatus()
        {
            while(_continuallyMonitorConnectionStatus)
            {
                _currentConnectionStatus=_getCurrentConnectionStatus();
            }
        }

        public void Dispose()
        {
            _continuallyMonitorConnectionStatus=false;
        }

        public enum ConnectionStatus
        {
            /// <summary>The device has no outbound IP connection</summary>
            NotConnected = 0,
            /// <summary>
            /// The device has an IP address but Internet availability has not or
cannot be
            /// verified
            /// </summary>
            PartiallyConnected = 1,
            /// <summary>The device has full bi-directional Internet
connectivity</summary>
            FullyConnected = 2,
            /// <summary>The device connectivity is in an unknown or indeterminate
state</summary>
            UnknownStatus = 99
        }
        public class ConnectionStatusEventArgs : System.EventArgs
        {
            private ConnectionStatus _connectionStatus =
ConnectionStatus.UnknownStatus;
            public ConnectionStatusEventArgs(ConnectionStatus connectionStatus) :
base()
            {
                _connectionStatus=connectionStatus;
            }
            public ConnectionStatus Status
            {
                get
                {
                    return _connectionStatus;
                }
            }
        }       
    }
}


-------------END CODE-----------------------

--------------------------------------------
Neil Macdonald
Senior Software Engineer
IntelliWhere Product Development Centre
Intergraph Corporation
Intergraph Mapping and Geospatial Solutions
Author
9 Feb 2005 7:30 AM
Jon Skeet [C# MVP]
Neil Macdonald [Intergraph]
<NeilMacdonaldIntergr***@discussions.microsoft.com> wrote:
> I have a class that runs a process that basically does some connection
> checking; I have a delegate setup to handle the status update event which
> will allow the client to give some kind of indication to the GUI.
>
> I get a NullReferenceException when I try to raise the event. Below is the
> class source...

If you haven't got any subscribers to the event, you will get a
NullReferenceException because you're calling a method on a null
delegate.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Are all your drivers up to date? click for free checkup

Author
9 Feb 2005 7:45 AM
Neil Macdonald [Intergraph]
Thanks, I realised that as I re-read my newsgroup post...don't you hate that

Show quoteHide quote
"Jon Skeet [C# MVP]" wrote:

> Neil Macdonald [Intergraph]
> <NeilMacdonaldIntergr***@discussions.microsoft.com> wrote:
> > I have a class that runs a process that basically does some connection
> > checking; I have a delegate setup to handle the status update event which
> > will allow the client to give some kind of indication to the GUI.
> >
> > I get a NullReferenceException when I try to raise the event. Below is the
> > class source...
>
> If you haven't got any subscribers to the event, you will get a
> NullReferenceException because you're calling a method on a null
> delegate.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

Bookmark and Share