我一直在獲取Null異常。我在程序中實例化了myHello,但它仍然給我這個錯誤。提前致謝。有誰知道我爲什麼會遇到空異常錯誤?
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]
{
get
{
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();
}
}
}
哇,你明白了。它現在像一陣微風。我把「(你好)(myHello [1])[1])」改爲「myHello [1] [1]」,因爲它給了我一些鑄造錯誤。再次感謝! – Vikyboss