samedi 25 avril 2015

Nested Internal Class with Readonly Hashtable throws Null ref exception.. on assignment

In my internal Game class, I have both a) defined another nested internal GamerTags class and b) defined a GamerTags[] _array variable.

In my internal GamerTags class (the nested class, for simplicity I left it out) I have a readonly Hashtable. After initialization, I still get a null ref exception when I try to assign a value. I'm not changing the pointer but the value only, do I need to add another initialization for the Hastable?

internal class GamerTags
    {
        private readonly Hashtable _privateReadonlyHashTable;
        private string _json;
        private string _hash;

        internal IDictionary privateReadonlyHashTable
        {
            get
            {
                return this._privateReadonlyHashTable;
            }
        }

        internal string Hash
        {
            get
            {
                this.EnsureHash();
                return this._hash;
            }
        }           

        internal object this[string key]
        {
            get
            {
                return this._privateReadonlyHashTable[key];
            }
            set //throws an exception
            {
                this._privateReadonlyHashTable[key] = value;
            }
        }

        internal string Json
        {
            get
            {
                if (string.IsNullOrEmpty(this._json))
                {
                    this._json = myJsonSerializer.Serialize(this._privateReadonlyHashTable);
                }
                return this._json;
            }
        }

        internal int X
        {
            get;
            private set;
        }

        internal int Y
        {
            get;
            private set;
        }

        internal GamerTags(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }

        private void EnsureHash()
        {
            bool flag = false;
            if (string.IsNullOrEmpty(this._hash))
            {
                if (flag)
                {
                    if (this.Json.Length < 100)
                    {
                        this._hash = this.Json;
                        return;
                    }
                    byte[] bytes = Encoding.ASCII.GetBytes(this.Json);
                    byte[] numArray = (new SHA1CryptoServiceProvider()).ComputeHash(bytes);
                    this._hash = Convert.ToBase64String(numArray);
                    return;
                }
                this._hash = this.FastHash();
            }
        }

        private string FastHash()
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (string key in this._privateReadonlyHashTable.Keys)
            {
                stringBuilder.Append(key);
                stringBuilder.Append("_");
                stringBuilder.Append(this._privateReadonlyHashTable[key]);
                stringBuilder.Append("_");
            }
            return stringBuilder.ToString();
        }
    }
}

Server Error in '/' Application.

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Init and assignment

        int a = 2; int b = 0;
        GamerTags GamerTag = new Game.GamerTags(a, b);
        GamerTag["Fuel"] = "Loaded";              //throws an exception
        GamerTag["EngineStatus"] = (boolIsItOn ? 1 : 0); //throw an exception too

Aucun commentaire:

Enregistrer un commentaire