Why does "{" throw a NullReferenceException in the static method?

This is a type of esoteric. I encountered a NullReferenceException while trying to open a form (in winforms design) 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(); } } 

... means a line with an opening brace. I ran into the same problem (but not with the participation of the winforms designer) in other projects, and I think it was a thread related problem, but I don't have code to replicate it.

Why is this happening and why does the exception stack trace point to a line with a curly brace?

Clarification: The null reference only exception occurs only in the winforms designer. When the application starts, it does not cause this error.

+6
c # static nullreferenceexception visual-studio winforms
source share
5 answers

I assume that line numbers are disabled (the actual reason for this is not so important), and the exception is actually thrown by this expression:

 method.DeclaringType.Namespace 

And the reason you can see the NullReference exception is because the expression new StackFrame(1) couple of previous lines can sometimes return an empty frame. A blank frame means that a call to .GetMethod() will return null, and there you go.

The reason you sometimes get an empty frame is because the compiler "right at the time" can choose built-in short, repeatedly called methods, such as one in your code. This will drop your call stack, at best you will get a higher level than you planned, or at worst (in your main method) there is no higher method and you will get null.

+4
source share

I assume that you have a static member initialization somewhere in your class, and that initializer throws a NullReferenceException . Also, I think you don't have a static constructor, so your object is marked as beforefieldinit, and so a NullReferenceException thrown, and your method that uses it is JITed.

Something like:

 public static class Logger { private static object x = InitObjectX(); private static object InitObjectX() { x.GetHashCode(); // Will throw since x is null. } 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(); } } 
+4
source share

The .pdb file containing the string may be outdated .

To fix this, rebuild the project and ensure that .pdb files are enabled in the project files. For C # projects, this can be configured on the "Assembly" tab by setting "Advanced" β†’ "Debugging information" only for a full or pdb file.

+3
source share

I think the problem is with calling the static method before building the static object. I fixed the problem in the winforms project by adding a static constructor .

If I remember correctly, the static constructor blocks the entire object at runtime.

+1
source share

Pointing to curly braces /, a seemingly incorrect line of code might someday happen. I think that just an exception occurred in the previous line of code, and Visual Studio for some reason selects the next line.

When guessing, the program may not break exactly on the line where the exception occurred due to any number of internal and external factors.

Sorry, I can’t explain it very well.

0
source share

All Articles