Application.Run中的NullReferenceException(new Main())

时间:2012-12-19 17:55:42

标签: c# winforms visual-studio-2010 nullreferenceexception

我在我的智慧结束。我有一个应用程序喜欢崩溃几分钟运行NullReferenceException(对象引用未设置为对象的实例)。通常我可以处理这个问题,但是当Visual Studio爆发时,它告诉我问题发生在Application.Run(new Main());。我不知道该怎么办才能开始解决这个问题。调用堆栈没有任何帮助,因为它只指向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()

3 个答案:

答案 0 :(得分:4)

这个异常实际上是在第三方的代码中引发的,但是Visual Studio没有向您展示。

查看异常的堆栈跟踪以查看完整堆栈。

答案 1 :(得分:1)

可能在这几个地方。将您的代码更改为

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

如果第一行崩溃,问题出在构造函数链的某处

如果第二个,那么它在初始化链中就会出现

开始在Main()类中的所有相关方法上捣毁红点,然后单步执行直至跟踪它为止。

答案 2 :(得分: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());
}