frankhardenberg
frankhardenberg

Reputation: 70

Why do I get a System.TypeInitializationException and how to fix it?

So I was working on a school project where I convert text to morsecode and vice versa. When I was still making up the layout everything worked fine and whenever I clicked one of the buttons I made in the first form they would redirect to another form and close the original one. Now I added a dictionary and some variables and all of a sudden I get this error. I tried googling already but I cannot seem to find the right solution for me. Hopefully someone can help me out here?

//Main Form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MorseCode
{
    public partial class Morsecode : Form
    {
        public Morsecode()
        {
            InitializeComponent();
        }

        private void ConvertToMorse_Click(object sender, EventArgs e)
        {
            this.Hide();
            ConvertToMorse Morse = new ConvertToMorse();
            Morse.ShowDialog();
        }

        private void ConvertToText_Click(object sender, EventArgs e)
        {
            this.Hide();
            ConvertToText Text = new ConvertToText();
            Text.ShowDialog();
        }

        private void Morsecode_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void ConvertToMorse_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void ConvertToText_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
    }
}

//Second Form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MorseCode
{
    public partial class ConvertToMorse : Form
    {
        string InputString = "";
        List<char> TextInput;
        static Dictionary<char, string> ToMorse = new Dictionary<char, string>()
        {
            {'A', ". _"},
            {'B', "_ . . ."},
            {'C', "_ . _ ."},
            {'D', "_ . ."},
            {'E', "."},
            {'F', ". . _ ."},
            {'G', "_ _ ."},
            {'H', ". . . ."},
            {'I', ". ."},
            {'J', ". _ _ _"},
            {'K', "_ . _"},
            {'L', ". _ . ."},
            {'M', "_ _"},
            {'N', "_ ."},
            {'O', "_ _ _"},
            {'P', ". _ _ ."},
            {'Q', "_ _ . _"},
            {'R', ". _ ."},
            {'S', ". . ."},
            {'T', "_"},
            {'U', ". . _"},
            {'V', ". . . _"},
            {'W', ". _ _"},
            {'X', "_ . . _"},
            {'Y', "_ . _ _"},
            {'Z', "_ _ . ."},
            {'0', "_ _ _ _ _"},
            {'1', ". _ _ _ _"},
            {'2', ". . _ _ _"},
            {'3', ". . . _ _"},
            {'4', ". . . . _"},
            {'5', ". . . . ."},
            {'6', "_ . . . ."},
            {'7', "_ _ . . ."},
            {'8', "_ _ _ . ."},
            {'9', "_ _ _ _ ."},
            {'.', ". _ . _ . _"},
            {',', "_ _ . . _ _"},
            {'?', ". . _ _ . ."},
            {'!', "_ . _ . _ _"},
            {'-', "_ . . . . _"},
            {'/', "_ . . _ ."},
            {':', "_ _ _ . . ."},
            {'\'', ". _ _ _ _ ."},
            {'-', "_ . . . . _"},
            {'}', "_ . _ _ . _"},
            {';', "_ . _ . _"},
            {'{', "_ . _ _ ."},
            {'=', "_ . . . _"},
            {'@', ". _ _ . _ ."},
            {'&', ". _ . . ."}
        };

        public ConvertToMorse()
        {
            InitializeComponent();
        }

        private void Input_TextChanged(object sender, EventArgs e)
        {

        }

        private void ConvertText_Click(object sender, EventArgs e)
        {
            InputString = Input.Text;

            foreach(char Text in InputString)
            {
                TextInput.Add(Text);
            }
        }

        private void Output_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

//Third Form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MorseCode
{
    public partial class ConvertToText : Form
    {
        static Dictionary<char, string> ToMorse = new Dictionary<char, string>()
        {
            {'A', ". _"},
            {'B', "_ . . ."},
            {'C', "_ . _ ."},
            {'D', "_ . ."},
            {'E', "."},
            {'F', ". . _ ."},
            {'G', "_ _ ."},
            {'H', ". . . ."},
            {'I', ". ."},
            {'J', ". _ _ _"},
            {'K', "_ . _"},
            {'L', ". _ . ."},
            {'M', "_ _"},
            {'N', "_ ."},
            {'O', "_ _ _"},
            {'P', ". _ _ ."},
            {'Q', "_ _ . _"},
            {'R', ". _ ."},
            {'S', ". . ."},
            {'T', "_"},
            {'U', ". . _"},
            {'V', ". . . _"},
            {'W', ". _ _"},
            {'X', "_ . . _"},
            {'Y', "_ . _ _"},
            {'Z', "_ _ . ."},
            {'0', "_ _ _ _ _"},
            {'1', ". _ _ _ _"},
            {'2', ". . _ _ _"},
            {'3', ". . . _ _"},
            {'4', ". . . . _"},
            {'5', ". . . . ."},
            {'6', "_ . . . ."},
            {'7', "_ _ . . ."},
            {'8', "_ _ _ . ."},
            {'9', "_ _ _ _ ."},
            {'.', ". _ . _ . _"},
            {',', "_ _ . . _ _"},
            {'?', ". . _ _ . ."},
            {'!', "_ . _ . _ _"},
            {'-', "_ . . . . _"},
            {'/', "_ . . _ ."},
            {':', "_ _ _ . . ."},
            {'\'', ". _ _ _ _ ."},
            {'-', "_ . . . . _"},
            {'}', "_ . _ _ . _"},
            {';', "_ . _ . _"},
            {'{', "_ . _ _ ."},
            {'=', "_ . . . _"},
            {'@', ". _ _ . _ ."},
            {'&', ". _ . . ."}
        };
        Dictionary<string, Char> text = ToMorse.ToDictionary(e => e.Value, e => e.Key);

        public ConvertToText()
        {
            InitializeComponent();
        }        

        private void Input_TextChanged(object sender, EventArgs e)
        {

        }

        private void ConvertText_Click(object sender, EventArgs e)
        {

        }

        private void Output_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

My IDE is Visual Studio 2017.

Upvotes: 1

Views: 2120

Answers (1)

Fabjan
Fabjan

Reputation: 13676

Usually System.TypeInitializationException means that some of static members that are initialized before anything else throw exception.

In your case the static dictionary ToMorse has a duplicate add statement here:

...
{'-', "_ . . . . _"},
{'/', "_ . . _ ."},
{':', "_ _ _ . . ."},
{'\'', ". _ _ _ _ ."},
{'-', "_ . . . . _"},  // bang! duplicate key
...

Dictionary throws when we try to add the same '-' key twice. Make sure all your Dictionary keys are unique and debug static members initialization to see that there is no error

Upvotes: 5

Related Questions