Event NullReferenceException  
Author Message
gdog300





PostPosted: Visual C# Language, Event NullReferenceException Top

I am getting a NullReferenceException when I fire an event. I know I should check for the NullReferenceException before I fire the event, but when I do that the event does not fire at all (obviously). And the event is suscribed to in another class. Any help would be appriciated, here is my code:

public class Main_Configuration : System.Windows.Forms.Form

{

public delegate void DebugEventHandler(string debug_message);

//declare the AE Button Event Handler

public event DebugEventHandler DebugMessageToRuntime;

private void DIOLabel_Click(object sender, System.EventArgs e)

{

this.DebugMessageToRuntime("Entering DIO Configuration Form");

}

}

public class Runtime : Runtime_Abstract

{

private Main_Configuration LW_Config;

public Runtime(int handle)

{

LW_Config = new Main_Configuration();

this.LW_Config.DebugMessageToRuntime += new Main_Configuration.DebugEventHandler (OnConfig_DebugMessageToRuntime);

}

private void OnConfig_DebugMessageToRuntime(string debug_message)

{

}

}

obviiously there is more to my code, but I only pasted what is relevant to my problem.



Visual C#5  
 
 
OmegaMan





PostPosted: Visual C# Language, Event NullReferenceException Top

It may be a timing issue...maybe your consumer hasn't subscribed when the first event is fired, hence it is null


 
 
gdog300





PostPosted: Visual C# Language, Event NullReferenceException Top

Thank you for the response,

I placed the subscription in the constructor of the class where I want the event handled. And the event fires on the click of a form button, so the subscription should have already taken place before the event fires


 
 
littleguru





PostPosted: Visual C# Language, Event NullReferenceException Top

Could you post the stack trace

 
 
gdog300





PostPosted: Visual C# Language, Event NullReferenceException Top

System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="LWLIF"
StackTrace:
at LWLIF.Main_Configuration.DIOLabel_Click(Object sender, EventArgs e) in C:\Documents and Settings\vm026982\Desktop\LW\LWLIF\Configuration\Main_Configuration.cs:line 1419
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Label.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at LWLIF.Main_GUI.Main() in C:\Documents and Settings\vm026982\Desktop\LW\LWLIF\GUI.cs:line 504
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

 
 
James Curran





PostPosted: Visual C# Language, Event NullReferenceException Top

From the stack trace it appears that you are doing a Application.Run(new Main_Configuration()); in LWLIF.Main_GUI.Main() which would mean that the instance of Main_Configuration which is calling DIOLabel_Click() is different from the one where you added the event handler.


 
 
gdog300





PostPosted: Visual C# Language, Event NullReferenceException Top

There is no call to Application.Run(new Main_Configuration()) in my program. There is Application.Run(new Main_GUI()) however, then it instantiates a Runtime class which instantiates the Main_Configuration. The event is supposed to fire from Main_Configuration to Runtime