Raising event from class results in NullReferenceException

G

Guest

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
 
J

Jon Skeet [C# MVP]

Neil Macdonald [Intergraph]
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.
 
G

Guest

Thanks, I realised that as I re-read my newsgroup post...don't you hate that

Jon Skeet said:
Neil Macdonald [Intergraph]
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top