c# - Nested Internal Class with Readonly Hashtable throws Null ref exception.. on assignment -
in internal game class
, have both a) defined nested internal gamertags class
, b) defined gamertags[] _array
variable.
in internal gamertags class
(the nested class, simplicity left out) have readonly hashtable
. after initialization, still null ref exception
when try assign value. i'm not changing pointer value only, need add initialization hastable?
internal class gamertags { private readonly hashtable _privatereadonlyhashtable; private string _json; private string _hash; internal idictionary privatereadonlyhashtable { { return this._privatereadonlyhashtable; } } internal string hash { { this.ensurehash(); return this._hash; } } internal object this[string key] { { return this._privatereadonlyhashtable[key]; } set //throws exception { this._privatereadonlyhashtable[key] = value; } } internal string json { { 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 instance of object. description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code.
exception details: system.nullreferenceexception: object reference not set instance of object.
init , assignment
int = 2; int b = 0; gamertags gamertag = new game.gamertags(a, b); gamertag["fuel"] = "loaded"; //throws exception gamertag["enginestatus"] = (boolisiton ? 1 : 0); //throw exception
indeed need instantiate _privatereadonlyhashtable field.
internal class gamertags { private readonly hashtable _privatereadonlyhashtable = new hashtable(); .. .. }
Comments
Post a Comment