why would a static constructor throw exception when it references to
a const string in another class.
class MyClass
{
static MyClass()
{
ExamineLog();
}
static ExamineLog()
{
FilePath = HttpContext.Current.Server.MapPath(Helper.LogConfiguration);
}
}
class Helper
{
public const string LogConfiguration= "\rootpath\counters.txt";
}
The exception thrown is object reference not set to an instance of an object. The stack trace points to the line where attempt is made to read the constant value. Any thoughts?
Thoughts:
HttpContextmight be nullHttpContext.Currentmight be nullHttpContext.Current.Servermight be nullFurther thoughts:
Currentis a static property of theHttpContextclass, soHttpContextis not an object reference, and it cannot be null. If you want to simplify your debugging, you can change the code like this (I’m assuming thatExamineLogshould have been declared as a void method):