c# - Do anyone know why I'm getting Null exception error? -
i keep on getting null exception. instantiated myhello inside program , still gives me error. in advance.
class hello { public dictionary<int, string> one; public dictionary<int, ushort> two; public dictionary<int, bool> three; public hello() { this.one = new dictionary<int, string>(); this.two = new dictionary<int, ushort>(); this.three = new dictionary<int, bool>(); } public dictionary<int, object> values { get; set; } public object this[int index] { { return this.values[index]; } set { this.values[index] = value; } } } class program { public static void myfun(ref hello hu) { hu[0] = "hello"; hu[1] = 25; hu[2] = true; } static void main(string[] args) { try { //program myprog = new program(); var myhello = new hello[2]; myhello[0] = new hello(); myhello[1] = new hello(); myhello[1][1] = 2; myfun(ref myhello[1]); console.writeline("" + (hello)(myhello[1])[1]); console.readkey(); } catch (nullreferenceexception ex) { console.writeline(ex.message); console.readkey(); } } }
values
never assigned default value , think trying access values
property before assigning value.
change constructor to:
public hello() { this.one = new dictionary<int, string>(); this.two = new dictionary<int, ushort>(); this.three = new dictionary<int, bool>(); this.values = new dictionary<int, object>(); }
Comments
Post a Comment