static TypeInitializationException

  • Thread starter Thread starter linaj
  • Start date Start date
L

linaj

Hi,

I have singleton class in C#:

class Logger
{
static Logger lInstance = new Logger();

private Logger() {}

public static Logger GetInstance() { return lInstance; }
}

When I want to access it from another assembly, like Logger l =
Logger.GetInstance(), I get TypeInitializationException ->
NullReferenceException. Any help?
 
Try initialising in a static constructor and catch the error there - see if
it still occurs and examine it. Might give you a better idea.

class Logger
{
static Logger lInstance;

static Logger()
{
try
{
lInstance = new Logger();
}
catch (TypeInitializationException tie)
{
// Examine contents here
}
}
}
 
I have singleton class in C#:

class Logger
{
static Logger lInstance = new Logger();

private Logger() {}

public static Logger GetInstance() { return lInstance; }
}

When I want to access it from another assembly, like Logger l =
Logger.GetInstance(), I get TypeInitializationException ->
NullReferenceException. Any help?

Well, I doubt that you get a TypeInitializationException from the above
code. I suspect you've got some code in the constructor, and that's
triggering the exception.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Oh I'm so sorry for your time, it's all my fault. <:-I

I worked with ConfigurationSettings.GetConfig() and had not provided
config file :( That's the cause why there was NullReferenceException in
my constructor.

Thanks you've found it out for me, even when I posted abolutely
different and working code :) Next time I'll place breakpoints
everywhere.
 
Back
Top