从一个TabControl的第一个TabPage切换到secondary TabPage,跳出如下异常:
An unhandled exception of type 'System.ArgumentNullException' occurred in system.windows.forms.dll
Additional information: Value cannot be null. 于是采用try...catch的方式debug到如下信息:
name "items" string
mess "Value cannot be null.\r\nParameter name: items" string
s "System.Windows.Forms" string
st null string
- ex {"Value cannot be null.\r\nParameter name: items" } System.ArgumentNullException
- System.ArgumentException {"Value cannot be null.\r\nParameter name: items" } System.ArgumentException
- System.SystemException {"Value cannot be null.\r\nParameter name: items"} System.SystemException
- System.Exception {"Value cannot be null.\r\nParameter name: items" } System.Exception
System.Object {System.ArgumentNullException} System.Object
_className null string
_COMPlusExceptionCode -532459699 int
_exceptionMethod <undefined value> System.Reflection.MethodBase
_exceptionMethodString null string
_helpURL null string
_HResult -2147467261 int
_innerException { } System.Exception
_message "Value cannot be null." string
_remoteStackIndex 0 int
_remoteStackTraceString null string
_source "System.Windows.Forms" string
+ _stackTrace {System.Array} System.Object
_stackTraceString null string
_xcode -532459699 int
_xptrs 0 int
HelpLink null string
HResult -2147467261 int
InnerException { } System.Exception
Message "Value cannot be null.\r\nParameter name: items" string
Source "System.Windows.Forms" string
StackTrace " at System.Windows.Forms.Control.SetVisibleCore(Boolean value)\r\n at System.Windows.Forms.Control.set_Visible(Boolean value)\r\n at
System.Windows.Forms.TabPage.set_Visible(Boolean value)\r\n at System.Windows.Forms.TabControl.UpdateTabSelection(Boolean uiselected)\r\n at
System.Windows.Forms.TabControl.OnSelectedIndexChanged(EventArgs e)\r\n at System.Windows.Forms.TabControl.WmSelChange(Message& m)\r\n at
System.Windows.Forms.TabControl.WndProc(Message& m)\r\n at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)\r\n at
System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam,
IntPtr lparam)\r\n at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n at
System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)\r\n at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message&
m)\r\n at System.Windows.Forms.Control.WmNotify(Message& m)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at
System.Windows.Forms.ScrollableControl.WndProc(Message& m)\r\n at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)\r\n at
System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam,
IntPtr lparam)\r\n at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n at
System.Windows.Forms.NativeWindow.DefWndProc(Message& m)\r\n at System.Windows.Forms.Control.DefWndProc(Message& m)\r\n at
System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at
System.Windows.Forms.TabControl.WndProc(Message& m)\r\n at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)\r\n at
System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam,
IntPtr lparam)\r\n at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32
pvLoopData)\r\n at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n at
System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n at System.Windows.Forms.Application.Run(Form mainForm)\r\n at
GISUDP.frmMain.Main() in e:\\qf\\emergencyframework\\gisudp20\\gisudp\\frmmain.cs:line 584" string
- TargetSite {System.Reflection.RuntimeMethodInfo} System.Reflection.MethodBase
+ [System.Reflection.RuntimeMethodInfo] {System.Reflection.RuntimeMethodInfo} System.Reflection.RuntimeMethodInfo
+ System.Reflection.MemberInfo {System.Reflection.RuntimeMethodInfo} System.Reflection.MemberInfo
Attributes 452 System.Reflection.MethodAttributes
CallingConvention 33 System.Reflection.CallingConventions
IsAbstract false bool
IsAssembly false bool
IsConstructor false bool
IsFamily true bool
IsFamilyAndAssembly false bool
IsFamilyOrAssembly false bool
IsFinal false bool
IsHideBySig true bool
IsOverloaded false bool
IsPrivate false bool
IsPublic false bool
IsSpecialName false bool
IsStatic false bool
IsVirtual true bool
+ MethodHandle {System.RuntimeMethodHandle} System.RuntimeMethodHandle
m_paramName "items" string
Message "Value cannot be null.\r\nParameter name: items" string
ParamName "items" string
_nullMessage "Value cannot be null." string
NullMessage "Value cannot be null." string
令我感兴趣的是中间这段:
StackTrace " at System.Windows.Forms.Control.SetVisibleCore(Boolean value)\r\n at System.Windows.Forms.Control.set_Visible(Boolean value)\r\n at
System.Windows.Forms.TabPage.set_Visible(Boolean value)\r\n at System.Windows.Forms.TabControl.UpdateTabSelection(Boolean uiselected)\r\n at
System.Windows.Forms.TabControl.OnSelectedIndexChanged(EventArgs e)\r\n at System.Windows.Forms.TabControl.WmSelChange(Message& m)\r\n at
System.Windows.Forms.TabControl.WndProc(Message& m)\r\n at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)\r\n at
System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam,
IntPtr lparam)\r\n at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n at
System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)\r\n at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message&
m)\r\n at System.Windows.Forms.Control.WmNotify(Message& m)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at
System.Windows.Forms.ScrollableControl.WndProc(Message& m)\r\n at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)\r\n at
System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam,
IntPtr lparam)\r\n at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)\r\n at
System.Windows.Forms.NativeWindow.DefWndProc(Message& m)\r\n at System.Windows.Forms.Control.DefWndProc(Message& m)\r\n at
System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at
System.Windows.Forms.TabControl.WndProc(Message& m)\r\n at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)\r\n at
System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam,
IntPtr lparam)\r\n at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32
pvLoopData)\r\n at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n at
System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n at System.Windows.Forms.Application.Run(Form mainForm)\r\n at
GISUDP.frmMain.Main() in e:\\qf\\emergencyframework\\gisudp20\\gisudp\\frmmain.cs:line 584" 可是想了半天,这里的set_Visible都不可能是客户代码的问题,我在网上也查了下,基本没有解决方案,另外发现有个老外超搞,说直接把TabPage的Visible置为true就好了,也不知道他的TabPage是什么版本的,竟然有public的Visible属性。我的程序是可以运行在不同GIS平台上的二次开发框架,这个问题是出在连MO时的,换了个平台就没有这个问题了,于是我从跟MO有关的代码出发,通过查找上面stacktrack里的items,终于发现了问题:原来在第二个TabPage的onload是我调用了如下代码:
this.cbTextField .Items .AddRange (items); 而items由于疏忽传了空值,所以产生了上面的异常,我估计TabControl在把secondary TabPage的visible设置为true时调用了它的onload消息,所以才抛出'System.ArgumentNullException'的异常。