Michael Mankus
Michael Mankus

Reputation: 4778

NullReferenceException at Application.Run(new Main())

I am at my wit’s end. I have an application that likes to crash a few minutes into running with a NullReferenceException (Object reference not set to an instance of an object). Normally I could deal with that just fine, but when Visual Studio breaks out, it’s telling me the issue is happening at Application.Run(new Main());. I have no idea what to do to begin trouble shooting this. The Call Stack doesn’t help any because it just points to that Application.Run(new Main()); line.

I went into the Exception window of Visual Studio (CTRL + ALT + E) and told it to report ALL exceptions to me. I was hoping it would find the issue before I get this NullReferenceException. But no luck.

What tools are available to me to help find this issue?

EDIT:

Here is the Call Stack as reported by 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()

Upvotes: 2

Views: 6525

Answers (3)

SLaks
SLaks

Reputation: 887469

This exception is actually being thrown in code in a third-party, but Visual Studio isn't showing you that.

Look at the exception's stack trace to see a full stack.

Upvotes: 4

Anlo
Anlo

Reputation: 3266

There might be an unhandled exception somewhere in your code, try to add the following to see if it gives more information:

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

Upvotes: 3

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

Could be in a few places this. Change your code to

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

If the first line falls over, problem is somewhere in the constructor chain

if the second then it's somwhere in the initialisation chain

Start battering red spots on all the relevant methods in the Main() class and stepping until you track it down.

Upvotes: 1

Related Questions