c# - 具有只读哈希表的嵌套内部类在分配时引发Null ref异常。

在我的internal Game class中,我既有a)定义了另一个嵌套的internal GamerTags class,又有b)定义了GamerTags[] _array变量。

在我的internal GamerTags class(嵌套类,为简单起见,我省略了它)中,我有一个readonly Hashtable。初始化后,当我尝试分配值时,仍然会收到null ref exception。我不是在更改指针,而是仅更改值,是否需要为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();
        }
    }
}

“/”应用程序中的服务器错误。

你调用的对象是空的。
说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中起源的更多信息。

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

初始化和分配
        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

最佳答案

实际上,您需要实例化_privateReadonlyHashTable字段。

internal class GamerTags
{
    private readonly Hashtable _privateReadonlyHashTable =  new Hashtable();

    ..
    ..
}

https://stackoverflow.com/questions/29862688/

相关文章:

java - 无法访问 org.eclipse.core.runtime.IProgressMoni

c# - 引用Shell32.dll?

c++ - Visual Studio中D8049错误的原因是什么?

r - 使用 R 包 brms 时出错

generics - TypeScript 泛型类 : Cannot assign to Gener

c# - 错误: [feature] requires a Business (or higher)

python - 在 Python 中不使用\n 的新行

c# - 分配的左侧必须是变量属性或索引器

macos - 指定编译器 Homebrew 安装

python - 无法运行Python脚本