473,320 Members | 2,000 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

static TypeInitializationException

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?

Jan 17 '06 #1
3 3673
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
}
}
}
<li***@centrum.cz> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
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?

Jan 17 '06 #2
<li***@centrum.cz> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 17 '06 #3
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.

Jan 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ethan | last post by:
I made a program that works fine as long as its on my machine, but as soon as i move it to a network drive it stops working. I'm still trying to run it on my machine just from a network drive and...
4
by: roger | last post by:
Here's a weird one... The code below works just fine when I build in DEBUG mode. Today, I tried to build my solution in RELEASE mode, and immediately fell over this problem - apparently...
1
by: sys | last post by:
I made a smart client that I purposed to offer on a web server. But when I try to run it by dint of Internet Explorer, I get the System.TypeInitializationException in the IEExec.exe thread. My...
1
by: nasambur | last post by:
Hi, i hav two classes as following; class1.cs ---------- namespace NAS { // fields // properties
18
by: Zytan | last post by:
I just got a TypeInitializationException exception. Why it is a common cause for this? The InnerException is System.FormatException, although I don't see anything requesting something to be...
4
by: BLUE | last post by:
VS .NET 2003 CF In this method I cannot catch exceptions thrown by Settings constructor (for example FileNotFoundException) but only an OutOfMemory exception due to Get returning void. Why??? ...
1
by: Art | last post by:
I have some Static libs which were intially implemented in VC6 and then converted to VC2005. When I try to use these in my managed C++ application, I am getting the following exception: An...
0
by: ArtiB | last post by:
Hi, I have VC++ Windows form application in VS 2005. I am not able to access some static libs(VC6 migrated) from this application. Getting exception of type 'System.TypeInitializationException' in...
0
by: =?Utf-8?B?TWFyY3VzIFNjaGFlZmVy?= | last post by:
Hi everybody, I'm working on my application for appr. 2 years and now suddenly I got an System.TypeInitializationException in one of my global modules. The error is thrown at the first call of a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.