|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Raising event from class results in NullReferenceExceptionI 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 Neil Macdonald [Intergraph]
<NeilMacdonaldIntergr***@discussions.microsoft.com> wrote: > I have a class that runs a process that basically does some connection If you haven't got any subscribers to the event, you will get a > 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... 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 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 >
Other interesting topics
Deadlock Problem
MSDN Exception Documentation PropertyGrid sees only public properties Killing a thread started with delegate.BeginInvoke() DataAdapter & SQLClient HTML Email Alert another session, how? Data Access in the Enterprise Library Determining .NET Install Directories Program error cordbg.exe 0x2 |
|||||||||||||||||||||||