TypeInitializationException when calling a static function from a timer

A

Andy

Beats me what is causing this. I've been banging my head against the
wall for several days now.

I have a csharp program that launches a long process that resides in
another application. My program has to wait for the output of the
other application, so I use a customized JobTimer based on the
System.Timers.Timer class to poll the other application for the status
of its job. I believe the timer and its callback function run on a
different thread than my program does, and the other application runs
on another machine.

When the application's job completes, one of my timer cycles detects
the job's status and the callbackmethod the timer calls resumes my
program's processing. My callbackmethod invokes a static method in
another non-static class. But, when this method is invoked, it throws
a TypeInitializationException without the function ever being
executed. The innerexception reports the following message:

Object reference not set to an instance of an object

The call looks like:

myNonStaticProgram{
myNonStaticCallbackMethod(){
myOtherNonStaticClass.staticFunction();
}
}

myOtherNonStaticClass{
public static staticFunction()
{
//debugger never steps into here
}
}


There are no further innerexceptions. What is puzzling is that the
same call does not throw a typeInitializationException if made from
anywhere else other than the timer's callback function. It almost
looks like the static object doesn't exist anymore by the time the
callbackfunction is activated.

Does anyone know what could be going wrong?

Andy
 
A

Andy

[...]
There are no further innerexceptions.  What is puzzling is that the
same call does not throw a typeInitializationException if made from
anywhere else other than the timer's callback function.  It almost
looks like the static object doesn't exist anymore by the time the
callbackfunction is activated.
Does anyone know what could be going wrong?

Not specifically, no.  How could we?  You didn't bother to post any
relevant code.

However, I can tell you more generally: the static constructor of the type
in question has a bug, and is trying to deference a null reference.

Set a break-point in the static constructor and step through.  Or just look
at the stack trace in the inner exception to see the line # where the
exception occurs.

Pete

Thanks for the insight Pete, you are correct.

I was going through all the posts for TypeInitializationException, and
they all said that this exception is thrown when a variable could not
be initialized because a null value was passed (and usually typecast)
into it. Yet, the line on which my code broke was a function call
which had nothing to do with setting variables.

What we found was the static function class had a constructor which
did contain a line of code that tried to set a variable to a null
value using a typecast. This line failed, and the constructor got
aborted. The abort caused the static instance of the class never to
get created. Once a static constructor fails, .NET will not attempt
to re-invoke it again.

So, in reality, there were really two exceptions. The first happened
long ago inside a static class when the program started and went
undetected. The second TypeInitializationException happened when a
line of code tried to use the missing class.

Often, static methods are called well inside the program long after
their creation, so the location of the TypeInitializationException
usually is far removed from were the problem first occured. The best
way to troubleshoot this kind of problem is to identify the name of
the class the static method belongs to (ie myClass.myStaticMethod();)
and then to immeadiatly review the classes constructors, including any
base class constructors (ie those of myClass). Test all variables (by
walking them with a debugger) in each constructor to see if any are
being set to null or an invalid construct. You aren't looking for why
your method (ie myStaticMethod) failed, and instead should be looking
for why the constructor didn't work.

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top