2012-12-19 94 views
1

我在我的智慧結束。我有一個應用程序喜歡在NullReferenceException(對象引用未設置爲對象實例)運行時崩潰幾分鐘。通常情況下,我可以處理這種情況,但是當Visual Studio發生故障時,它告訴我問題發生在Application.Run(new Main());。我不知道該怎麼做才能開始解決這個問題。 Call Stack沒有任何幫助,因爲它只是指向Application.Run(new Main());行。NullReferenceException在Application.Run(new Main())

我進入Visual Studio的異常窗口(CTRL + ALT + E),並告訴它向我報告所有異常。我希望在我得到這個NullReferenceException之前找到問題。但沒有運氣。

有哪些工具可以幫助我找到這個問題?

編輯:

這裏是調用堆棧報道由Visual Studio:

StackTrace: 
     at GMap.NET.WindowsForms.GMapOverlay.DrawRoutes(Graphics g) 
     at GMap.NET.WindowsForms.GMapOverlay.Render(Graphics g) 
     at GMap.NET.WindowsForms.GMapControl.OnPaintOverlays(Graphics g) 
     at GMap.NET.WindowsForms.GMapControl.OnPaint(PaintEventArgs e) 
     at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer) 
     at System.Windows.Forms.Control.WmPaint(Message& m) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ScrollableControl.WndProc(Message& m) 
     at System.Windows.Forms.UserControl.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(IntPtr 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 Foxhunt.Client.Program.Main() in C:\Users\Michael\Documents\Visual Studio 2010\Projects\Foxhunt.Client\Program.cs:line 18 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly 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, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
+2

請粘貼整個堆棧跟蹤。 – SLaks

+0

此外,當您只是在Application.Run行和代碼的退出行上設置斷點時會發生什麼情況...如果有事情迫使您的應用程序重新啓動,那麼可能會顯示它... – Rikon

+0

按請求添加的信息。我只能在應用程序第一次運行時斷開Application.Run。下次發生任何事情時,我得到'NullReferenceException'。 –

回答

4

這種異常實際上是被扔在代碼中的第三方,但Visual Studio沒有向你顯示。

查看異常的堆棧跟蹤以查看完整的堆棧。

1

可能是在一些地方這一點。更改您的代碼

var mf = new Main(); 
Application.Run(mf); 

如果第一行倒了,問題是什麼地方在構造函數鏈

如果第二那麼它somwhere在初始化鏈

開始毆打上的所有紅點Main()類中的相關方法並逐步進行,直到您追蹤它。

+0

它在第二行發生衝突。我知道構造函數是好的,因爲在應用程序發生前幾分鐘使用該應用程序(這使得調試變得更有趣,因爲我必須等待它)。 –

1

有可能的地方是一個未處理的異常在你的代碼,嘗試添加下面的,看它是否提供了更多信息:

static void Main() 
{ 
    Application.ThreadException += ThreadException; 
    AppDomain.CurrentDomain.UnhandledException += UnhandledException; 

    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Application.Run(new Main()); 
} 

private static void ThreadException(object sender, ThreadExceptionEventArgs e) 
{ 
    MessageBox.Show(e.Exception.ToString()); 
} 

private static void UnhandledException(object sender, UnhandledExceptionEventArgs e) 
{ 
    MessageBox.Show(e.ExceptionObject.ToString()); 
} 
相關問題