简体   繁体   中英

Unhandled Type Exception(System.TypeInitializationException)

As the title says, Visual Studio is throwing an exception when I execute my program. The exact error:

An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll

As I'm rather new to using Visual Studio, and C# for that matter, I was unable to discern what the issue was. I did Google, but none of the information I found assisted the recovery of my excepted program.

Program code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;



namespace Game {
    public struct entity {
        int XP,
            HP,
            mana,
            luck,
            strength,
            defense;
        float modStrength,
            modDefense;
        string stance;
    }

    public class Game {
        private  entity enemy;
        private static Dictionary<string, int> playerData =
            new Dictionary<string, int>();
         public static string[] entityPool =
            new StreamReader(Properties.Resources.Entities).ToString().Split('?');

        static void Main (string[] args) {
            instancePlayer();
            Console.ReadKey();
        }   
        private static void instancePlayer () {
            string[] playerDataDummy = entityPool[0].Trim().Split(';');

            foreach (string s in playerDataDummy) {
                string[] indivArr = s.Split(' ');
                playerData.Add(indivArr[0], Convert.ToInt16(indivArr[1]));
            }

            foreach (KeyValuePair<string, int> s in playerData) {
                Console.WriteLine("[{0}] {1}", s.Key, s.Value);
            }
        }

        private void instanceEnemy () {

        }
    }
}

I have been able to narrow the issue down to this line, though...

public static string[] entityPool = new StreamReader(Properties.Resources.Entities).ToString().Split('?');

That's about as much as I've been found out; removing the initialization of that, and all it's reference, nullifies the issue. But alas, I do need it there.

Take a look at the streamreader class on MSDN . There are several constructors for it and a simple example for reading from a text file. I'm not sure what you have in Properties.Resources.Entities (a file path I assume). Assuming this is a valid file path or stream and the constructor isn't throwing the error, you are creating the streamreader then calling .ToString() which gives you a string representation of the StreamReader object, not the contents of the file or stream. This is probably not what you are expecting. The sample on the MSDN page should help you with using the StreamReader class.

This line is wrong.

public static string[] entityPool = new StreamReader(Properties.Resources.Entities).ToString().Split('?');

What you probably meant was to read all the contents of the stream. Something like this.

StreamReader sr = new StreamReader(Properties.Resources.Entities);
string[] entityPool = sr.ReadToEnd().Split('?');

Better to put this code in the constructor with a try-catch rather than in an initialiser.

It appears that a StreamReader was not necessary to access the contents of the file.

string[] entityPool = Properties.Resources.Entities.split('?'); works as I previously intended.

I appreciate the responses, All.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM