This one is sort of esoteric. I ran into a NullReferenceException while trying to open a form (in the winforms designer) in a winforms project in visual studio 2008. The stack trace points to the fourth line of the following code:
public static class Logger
{
public static void LogMethodEnter()
{
var frame = new StackFrame(1);
var method = frame.GetMethod();
Trace.TraceInformation("{0}.{1}.{2}()", method.DeclaringType.Namespace, method.DeclaringType.Name, method.Name);
Trace.Indent();
}
public static void LogMethodExit()
{
Trace.Unindent();
}
}
...meaning the line with the opening curly brace. I've run into the same issue (but not involving the winforms designer) on other projects, and I think it was a threading related issue, but I don't have the code to replicate it.
Why does this happen and why does the exception stack trace point to the line with the curly brace?
Clarification: The null reference exception only happens in the winforms designer. When the application is run, it doesn't throw that error.