0

Maybe it's an easy one, but I'm just starting with C# and can't figure out what I did wrong.

I was making a simple ping pong application, it worked properly, but suddenly a piece of code started throwing this error. Then I changed the game's main Form to a raw window's form, but the crash is still there.

Form form = new Form();

static void Main() {
  Application.Run(form); //<-- Throwing the Crash
}

The next image says it all, I really appreciate any help, even the slighter one. Thanks ^^

The Error thrown

2
  • Have you tried stepping through the code in the form? Commented Nov 15, 2015 at 2:08
  • What do you mean by stepping? I'm using the raw windows form, with no modifications. Anyways, this used to work fine this morning, but now it's crashing, I did no modifications neither to windows form or to the form I was using before. Commented Nov 15, 2015 at 2:12

2 Answers 2

2

Look at the error message. The type initializer for Program is what's throwing the exception. That type initializer has a few things happening, including:

new StartScreen()
new Settings()
new SettingsManager()

and possibly more that we don't see in the screen shot.

Something in the type initializer for Program is failing. Examine the exception for more information. Also, move some of that logic to constructors (static or instance as appropriate) to make it easier to debug.

(Or as I like to say, a TypeInitializationException is a fancy way of saying, "You have too much happening outside of methods.")

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

1 Comment

Thanks a lot! the problem was in 'new Settings()'. I used to have only settings and settingsManager constructor as static, I added the other ones trying to fix this error.
-1

The line

Form form = new Form();

is executed when you first access the variable form. In this case it's in this line:

Application.Run(form);

So the constructor of the class Form threws an error

4 Comments

This is not true at all.
Hmm, nope, that's not the issue. I've tried too declaring the form field in the Main method, still the same crash. Thanks anyways ^.^
"So the constructor of the class Form threws an error" - Then why would the error say that it's being thrown by the class Program?
Oh, it seems I missed the other static variables because I was only looking on the form variable, sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.