简体   繁体   English

非静态类中的类型初始化失败

[英]Type Initialization Failed In Non-Static Class

I've got this nagging issue that I need to fix for a major project of mine. 我遇到了一个棘手的问题,需要为我的一个大型项目解决。 I'm trying to reference a static variable (owned by a static class) from a non-static class. 我正在尝试从非静态类引用静态变量(由静态类拥有)。 When I try to do this, it throws the exception 'The type initializer for 'Eternal_Continent.PSTATS' threw an exception.' 当我尝试执行此操作时,它将引发异常“'Eternal_Continent.PSTATS'的类型初始值设定项引发了异常”。

The inner exception reads 内部异常读取

Object reference not set to an instance of an object. 你调用的对象是空的。

Here's my PSTATS class 这是我的PSTATS课

public static class PSTATS
    {
        static Locations Locations;
        public static string name = "";
        public static int health = 100;
        public static int dmg = 1;
        public static int mana = 100;
        public static int hpotion = 3;
        public static int mpotion = 3;
        public static int def = 1;
        public static int level = 1;
        public static int xp = 0;
        public static float tradereward = 1.0f;
        public static string employer = "Knight Artemis";
        public static misc.Quest currentquest;
        public static misc.NPC currentnpc = null;
        public static int npcindex = 0;
        public static misc.Location currentlocation = Locations.Ardimir;
        public static string reward = "Charisma + 1";
        public static bool finding = false;
        public static string questreward = "G";
        public static int kills = 0;
        public static int gold = 0;
        // Has found item

        public static bool found = false;

        //Has job

        public static bool job = false;
        public static int reqkills = 0;
        public static int reqgold = 0;
        public static int dex = 1;
        public static int str = 1;
        public static int itl = 1;
        public static int cha = 1;

        public static bool existingdialog = false;
    }

And here's the non-static class calling it 这是调用它的非静态类

public class Weapon
        {
            #region
            public Weapon(string name, string desc, int dmg, int mana)
            {

                Name = name;
                Desc = desc;
                Dmg = Convert.ToInt32(dmg * (PSTATS.level / 0.9));
                Manausage = mana;
            }
            public int Manausage;
            public int manausage
            {
                get
                {
                    return Manausage;
                }
                set
                {
                    Manausage = value;
                }
            }
            public string Name;
            public string name
            {
                get
                {
                    return Name;
                }
                set
                {
                    Name = value;
                }
            }
            public string Desc;
            public string desc
            {
                get
                {
                    return Desc;
                }
                set
                {
                    Desc = value;
                }
            }
            public int Dmg;
            public int dmg
            {
                get
                {
                    return Dmg;
                }
                set
                {
                    Dmg = Convert.ToInt32(value * (PSTATS.level / 0.9));
                }
            }

            #endregion
        }

Thanks to anyone who can help me fix this. 感谢任何可以帮助我解决此问题的人。

I copied the exact code you have creating the two classes. 我复制了创建两个类的确切代码。
I then created an object in main: Weapon w = new Weapon("n", "d", 6, 3); 然后,我在main中创建了一个对象:Weapon w = new Weapon(“ n”,“ d”,6,3); It was successful and there were no problems using the static PSTATS variables. 它是成功的,并且使用静态PSTATS变量没有任何问题。

The only changes I made were deleting Locations and misc variables in PSTAT which are not needed for creating a Weapon and allowed me to run the test. 我所做的唯一更改是删除了PSTAT中的Locations和misc变量,这些变量是创建武器不需要的,并允许我运行测试。

I think we're still missing some of your code, but my gut tells me it has to do with the first member in your PSTATS class called Locations . 我认为我们仍然缺少您的一些代码,但是我的直觉告诉我,这与您的PSTATS类中的第一个称为Locations成员有关。 It would appear that Locations is an enum which means you shouldn't need a reference to it. 看来Locations是一个枚举,这意味着您不需要引用它。 Try removing that member and perhaps post the code pertaining to misc and the Locations enum. 尝试删除该成员,然后发布与miscLocations枚举有关的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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