Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on an application where we can open child modules using multiple tab display. But when someone switches between the tabs so frequently or clicks on a tab more than once then sometimes I got fatal nullreference exception which further crash my whole application. I have applied complete exception handling but this exception is not getting caught anywhere. I think this might be some windows related exceptions which are happening on the kernel. I am posting the call stack also and to get the info on the exception I used profiling tool.

System.NullReferenceException occurred Message: Exception thrown: 'System.NullReferenceException' in System.Windows.Forms.dll Additional information: Object reference not set to an instance of an object.

This call stack is above of the below call stack - this I got from the profiling tool.


>class="System.Windows.Forms.UnsafeNativeMethods" method="WaitMessage()"
class="System.Windows.Forms.Application+ComponentManager" method="System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr,int,int)"
class="System.Windows.Forms.Application+ThreadContext" method="RunMessageLoopInner(int,System.Windows.Forms.ApplicationContext)"
class="System.Windows.Forms.Application+ThreadContext" method="RunMessageLoop(int,System.Windows.Forms.ApplicationContext)"
class="System.Windows.Forms.Application" method="RunDialog(System.Windows.Forms.Form)"
class="System.Windows.Forms.Form" method="ShowDialog(System.Windows.Forms.IWin32Window)"
class="System.Windows.Forms.Form" method="ShowDialog()"
class="OuterNamespace.Portal.Utilities.Forms.MessageBoxEx" method="Show(System.String,System.String,System.Windows.Forms.MessageBoxButtons,System.Windows.Forms.MessageBoxIcon)"
class="OuterNamespace.Portal.Utilities.Forms.MessageBoxEx" method="Show(System.String,System.Windows.Forms.MessageBoxButtons,System.Windows.Forms.MessageBoxIcon)"
class="OuterNamespace.Portal.Components.ExceptionHandler" method="Handle(System.Exception,System.String,OuterNamespace.Portal.Constants.Logging.LogType)"
class="OuterNamespace.Portal.Components.ExceptionHandler" method="HandleException(System.Exception,boolean,OuterNamespace.Interfaces.RuntimeInformation.IRuntimeInformationManager)"
class="OuterNamespace.Exception.Manager.ExceptionManager" method="CallUnhandledExceptionHandler(System.Exception)"
class="OuterNamespace.Exception.Manager.ExceptionManager" method="HandleUnhandledException(System.Exception)"
class="OuterNamespace.Application.ApplicationContext" method="Application_ThreadException(System.Object,System.Threading.ThreadExceptionEventArgs)"
class="System.Windows.Forms.Application+ThreadContext" method="OnThreadException(System.Exception)"
class="System.Windows.Forms.Control" method="WndProcException(System.Exception)"
class="System.Windows.Forms.Control+ControlNativeWindow" method="OnThreadException(System.Exception)"
class="System.Windows.Forms.NativeWindow" method="Callback(System.IntPtr,int,System.IntPtr,System.IntPtr)"
class="System.Windows.Forms.UnsafeNativeMethods" method="SetFocus(System.Runtime.InteropServices.HandleRef)"

------------------------------------------------------------------------
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.FocusActiveControlInternal() Unknown
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.WmSetFocus(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.UserControl.WmSetFocus(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.UserControl.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Unknown
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.FocusActiveControlInternal() Unknown
System.Windows.Forms.dll!System.Windows.Forms.ContainerControl.WmSetFocus(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Form.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.WndProc(ref System.Windows.Forms.Message m) Unknown
Module.dll!MyNamespace.Wrappers.WindowWrapper.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.WndProc(ref System.Windows.Forms.Message m) Unknown
Module.dll!MyNamespace.Wrappers.WindowWrapper.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.WndProc(ref System.Windows.Forms.Message m) Unknown
Module.dll!MyNamespace.Wrappers.WindowWrapper.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.WndProc(ref System.Windows.Forms.Message m) Unknown
Module.dll!MyNamespace.Wrappers.WindowWrapper.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.WndProc(ref System.Windows.Forms.Message m) Unknown
Module.dll!MyNamespace.Wrappers.WindowWrapper.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.WndProc(ref System.Windows.Forms.Message m) Unknown
Module.dll!MyNamespace.Wrappers.WindowWrapper.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.WndProc(ref System.Windows.Forms.Message m) Unknown
Module.dll!MyNamespace.Wrappers.WindowWrapper.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.WndProc(ref System.Windows.Forms.Message m) Unknown
Module.dll!MyNamespace.Wrappers.WindowWrapper.WndProc(ref System.Windows.Forms.Message m) Unknown
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Unknown
[Native to Managed Transition]
[Managed to Native Transition]
Module.dll!MyNamespace.FocusController.FixMouseDoubleClickError.AnonymousMethod__68_0() Unknown
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.Delegate.DynamicInvokeImpl(object[] args) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallbackDo(System.Windows.Forms.Control.ThreadMethodEntry tme) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(object obj) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallback(System.Windows.Forms.Control.ThreadMethodEntry tme) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallbacks() Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control caller, System.Delegate method, object[] args, bool synchronous) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.Invoke(System.Delegate method, object[] args) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.Invoke(System.Delegate method) Unknown
Module.dll!MyNamespace.FocusController.FixMouseDoubleClickError() Unknown
Module.dll!MyNamespace.FocusController.UpdateAsyncIff.AnonymousMethod__91_0() Unknown
[Native to Managed Transition]
[Managed to Native Transition]
mscorlib.dll!System.Delegate.DynamicInvokeImpl(object[] args) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallbackDo(System.Windows.Forms.Control.ThreadMethodEntry tme) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(object obj) Unknown
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallback(System.Windows.Forms.Control.ThreadMethodEntry tme) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.InvokeMarshaledCallbacks() Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control caller, System.Delegate method, object[] args, bool synchronous) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.Invoke(System.Delegate method, object[] args) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Control.Invoke(System.Delegate method) Unknown
Module.dll!MyNamespace.FocusController.FixMouseDoubleClickError() Unknown
Module.dll!MyNamespace.FocusController.HandleCpmModalForegroundWindow(System.IntPtr appForegroundWinHdl) Unknown
Module.dll!MyNamespace.FocusController.HandleForegroundWindowChange(System.IntPtr foregroundDetectedHdl) Unknown
Module.dll!MyNamespace.FocusController.ForegroundWindowChangeDetectorOnForegroundWindowChanged(object sender, MyNamespace.Detectors.ForegroundEventArgs foregroundEventArgs) Unknown
Module.dll!MyNamespace.Detectors.ForegroundWindowChangeDetector.OnForegroundWindowChanged(MyNamespace.Detectors.ForegroundEventArgs e) Unknown
Module.dll!MyNamespace.Detectors.ForegroundWindowChangeDetector.ForegroundCallback(System.IntPtr eventHook, uint eventType, System.IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) Unknown
[Native to Managed Transition]
[Managed to Native Transition]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(System.IntPtr dwComponentID, int reason, int pvLoopData) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(int reason, System.Windows.Forms.ApplicationContext context) Unknown
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(int reason, System.Windows.Forms.ApplicationContext context) Unknown
Portal.Desktop.exe!OuterNamespace.Portal.Desktop.Program.Main() Unknown

What I have tried:

I am not able to reach to the problem yet so not able to find the workaround or any fix for this.
Posted
Updated 21-Jul-17 2:29am
Comments
Richard MacCutchan 21-Jul-17 8:30am    
You need to trace back to the point in your code that caused the exception. Most likely by passing a null reference as a parameter to a Windows method.

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
leonidasvijay 21-Jul-17 8:38am    
I have debugged the code with visual studio too but when ever the exception occurs it wont stop at any point. I have added a lot exception handling with generic exception class but still got no luck. And my application is consist of more than one application. And i am using only a library where this exception is coming. And moreover through the profiling tool I somehow got the above stacktrace and found the exception is finally reached to the main method and that main method is under the control of other application whose code is not accessible to me.
OriginalGriff 21-Jul-17 8:44am    
Then you need to start with the source code for that application, or the code that causes it to run. Unless you can see what it is actually doing, or what you are passing to it, you can't fix a problem ... a stack trace tells you nothing without the code to match it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900