【问题标题】:NullReferenceException at Application.Run(new Main())Application.Run (new Main()) 处的 NullReferenceException
【发布时间】:2012-12-07 03:37:27
【问题描述】:

我无计可施。我有一个应用程序,它喜欢在运行 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()

【问题讨论】:

  • 请粘贴整个堆栈跟踪。
  • 在“异常”对话框中选择 NullReferenceException:抛出异常时 - 进入调试器。
  • 您在表单中使用了非 .NET 控件,可能是 ActiveX 控件。您需要启用非托管调试并打开 Microsoft 符号服务器才能查看更多信息。
  • 禁用仅我的代码并再次查看调用堆栈。还要查看 exception 的堆栈跟踪。
  • @SLaks 你摇滚!!!!!!!!!!!!我没有意识到您可以深入了解异常的堆栈跟踪。它发生在我使用的第三方库中,我只有编译好的 dll。不是源头。我想我能解决这个问题。

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


【解决方案1】:

此异常实际上是在第三方代码中引发的,但 Visual Studio 并未向您显示。

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

【讨论】:

    【解决方案2】:

    您的代码中可能存在未处理的异常,请尝试添加以下内容以查看是否提供更多信息:

    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());
    }
    

    【讨论】:

      【解决方案3】:

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

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

      如果第一行失败,问题出在构造函数链中

      如果是第二个,那么它在初始化链中的某个地方

      开始在 Main() 类中的所有相关方法上打红点,并逐步跟踪它。

      【讨论】:

      • 它在第二行敲响了。我知道构造函数很好,因为我在应用程序发生之前使用了几分钟(顺便说一句,这使得调试它变得更加有趣,因为我必须等待它)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多