0

I get into a TypeInitializationException. The inner exception is a DivideByZeroException. Looking at the stack trace for the inner exception, I get this:

à System.Decimal.FCallDivide(Decimal& d1, Decimal& d2)
à System.Decimal.op_Division(Decimal d1, Decimal d2)
à DimensionnelJDP.IncertitudeObject..cctor() dans \IncertitudeObject.cs:ligne 108

The code that triggers the exception is this:

IncertitudeObject io = new IncertitudeObject();
io.Compute_E( Config, ( (EmpilementObject)row[ "Empilement" ] ).pile ); //Exception here

My investigations on the issue lead me to see that, when I look inside io, there are bunch of question marks instead of a bunch of variables. Question marks?

The same exception happens regardless of what function I call (including when calling io.ToString()), and io is always full of question marks.

I figured IncertitudeObject doesn't get initialized correctly, though how is beyond me. It doesn't have a constructor, so I'm assuming it uses the default implicit parameterless constructor of the language. So really, I don't know what is happening here.

It was working fine last week, there wasn't significant changes I can remember. The same setup also works on other projects of my solution.


Here is a quick look at my struct:

public struct IncertitudeObject
{
    private decimal ua;
    public decimal Ua{ get { return ua; } }
    //And a dozen more like it

    private static decimal[] Ref_UA_R = { 4.1M / 1000.0M, 8.2M / 1000.0M, 0.0M };
    //This is line 108 that the stack trace points to
    //And there are also a dozen more like it

    //Bunch of functions that do things that are never called when the exception happens

    //Distinct lack of constructor
}

EDIT: So I found the problem. I didn't know about that whole static constructor thing. Apparently, the order of static field was really important. Had to switch from:

    private static decimal[] Ref_UB2_E = { R2 * 10.0M / ( 2.0M * R3 ) / 1000.0M, 20.0M / ( 2.0M * R3 ) / 1000.0M, 50.0M / ( 2.0M * R3 ) / 1000.0M };
    private static decimal R2 = (decimal)Math.Sqrt( 2 );
    private static decimal R3 = (decimal)Math.Sqrt( 3 );

to:

    private static decimal R2 = (decimal)Math.Sqrt( 2 );
    private static decimal R3 = (decimal)Math.Sqrt( 3 );
    private static decimal[] Ref_UB2_E = { R2 * 10.0M / ( 2.0M * R3 ) / 1000.0M, 20.0M / ( 2.0M * R3 ) / 1000.0M, 50.0M / ( 2.0M * R3 ) / 1000.0M };

In hindsight, I should have seen this.

3
  • You should initialize variable 'ua' to zero (or some other default value). You also need to test if 'ua' is zero before dividing by zero. You either need to initialize 'ua' to a value other than zero, or you are missing code that sets 'ua' to a value before dividing by zero. Commented Jun 28, 2016 at 10:21
  • 1
    //And there are also a dozen more like it -> looks like one of those has a division by zero in it. Take a look at them or post them here for us to see. Commented Jun 28, 2016 at 10:26
  • 1
    @jdweng The exception is from the static constructor, instance fields are irrelevant. Commented Jun 28, 2016 at 10:29

1 Answer 1

1

Your exception is in the static constructor. You did not write one explicitely, but one was generated to hold the initialization of static fields.

private static decimal[] Ref_UA_R = { 4.1M / 1000.0M, 8.2M / 1000.0M, 0.0M };

The code in those initializers is placed in a static constructor by the compiler.

While this particular line wont throw a DivideByZeroException, some line like it apparently is.

Sign up to request clarification or add additional context in comments.

1 Comment

Welp, that was it. The order of those static fields was really important apparently. I feel slightly stupid.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.