System.TypeInitializationException
Asked Answered
S

3

5

I am writing tests to test Infopath Forms to open in Form Control, my test method is as

[TestMethod]
public void Validate_OpenInfopathInFormControl()
{
    Helper.OpenForm();
    //Other Code    
}

I have written Helper class as

public class Helper
{  
    public static void OpenForm()
    {
        //Code to Open Form
    }
}

But everytime I execute this code, this gives me:

Test method InfoPathTest.TestAPI.Validate_OpenInfopathInFormControl threw exception: System.TypeInitializationException: The type initializer for 'InfoPathTest.Helpers.Helper' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object..

When I try to debug, it fails when Helper class needs to be initialized. This is really eating my head, is there any solution for this?

Here is the complete helper class:

namespace InfoPathTest.Helpers
{
    public class Helper
    {
    //This is the form i need to OPEN
        private static MainForm f =  new MainForm();
        private static bool _isOpen = false;

        public static bool isOpen
        {
            set { _isOpen = value; }
            get { return _isOpen; }
        }

        public static void OpenForm()
        {
            try
            {
                f.Show();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            _isOpen = true;

        }

        public static void CloseForm()
        {
            f.Hide();
        }
    }
}
Smatter answered 20/8, 2010 at 22:29 Comment(5)
Could paste the helper class with the constructor and variables?Enterpriser
You'll have to show us what code is in the static constructor for your Helper class (public static Helper()) as it's something in there that's causing the exceptionDowntown
i have updated above. There is no static constructorSmatter
throw ex? Just do "throw;"Ethiopian
This can result from a bad Visual Studio runtime state; try closing it and opening it again.Mandamandaean
D
6

Your test calls Helper.OpenForm() and as you have no static constructor, the only thing I can see that would cause the exception to be thrown is:

private static MainForm f =  new MainForm();

Therefore something in the constructor for MainForm is likely throwing an exception. Put a breakpoint on the first line of the constructor for MainForm and step through until you see where the exception is thrown.

Alternatively you might find it easier to determine what the problem is, at least initially, by writing a new test you can step through that calls new MainForm() directly:

[TestMethod]
public void Validate_OpenInfopathInFormControl()
{
    var form = new MainForm();
}

Put a breakpoint on the only line of the test and step into the constructor to determine why it's throwing a NullReferenceException.

Downtown answered 20/8, 2010 at 22:43 Comment(1)
Thanks, yes you are right. There is a problem in constructor for MainForm and I am working in it!Smatter
B
3

The type initialiser, in this case, is where your static fields are initialised; That is, these two lines:

private static MainForm f =  new MainForm();
private static bool _isOpen = false;

The initialisation of a bool isn't going to cause any kind of exception, so it's highly likely that the source of the error is in the MainForm constructor.

Does the TypeInitializationException object contain any inner exceptions? If so, they should give you more info about the real cause of the error.

Bartholomeus answered 20/8, 2010 at 22:44 Comment(0)
E
2

You have an error in your static constructor (they are called Type Initializers). The inner exception is a NullReference exception. If you post your code we might be able to help you.

The rules determine when type initializers get run are complex, but it is guaranteed that they are run before you access the type in any way. It might not be directly obvious to you that you have a type initializer on your Helper class because you might use implicit initialization:

public class Helper
{   
    static int i = 10; // This assignment will end up in a type initializer
    static Helper()
    {
        // Explicit type initializer
        // The compiler will make sure both, implicit and explicit initializations are run
    }
}
Electromechanical answered 20/8, 2010 at 22:34 Comment(1)
I knew this was coming :-) Started expanding before I saw that comment. Hope it clears things up.Electromechanical

© 2022 - 2024 — McMap. All rights reserved.