2013-03-07 83 views
2

我正在嘗試訪問靜態列表的值。但是,當我嘗試這樣做時,會引發此異常。System.TypeInitializationException:「提示」的類型初始值設定項引發異常

System.TypeInitializationException:「提示」的類型初始值設定項引發異常。 - - > System.NullReferenceException:未將對象引用設置爲對象的實例

帶有列表的類。

public static class Tips 
{ 
    //private List<Tip> roadtips = new List<Tip>(); 
    public static List<Tip> tips { get; set; } 

    static Tips() 
    { 
     tips.Add(new Tip("Don't use your mobile phone whilst driving", "Making or receiving a call, even using a 'hands free' phone, can distract your attention from driving and could lead to an accident. ")); 
     tips.Add(new Tip("Children", "Children often act impulsively, take extra care outside schools, near buses and ice cream vans when they might be around.")); 
     tips.Add(new Tip("Take a break", "Tiredness is thought to be a major factor in more than 10% of road accidents. Plan to stop for at least a 15 minute break every 2 hours on a long journey.")); 
     tips.Add(new Tip("Don't drink and drive", "Any alcohol, even a small amount , can impair your driving so be a safe driver don't drive and drive.")); 
     tips.Add(new Tip("Anticipate ", "Observe and anticipate other road users and use your mirrors regularly.")); 
     tips.Add(new Tip("Use car seats ", "Child and baby seats should be fitted properly and checked every trip.")); 
     tips.Add(new Tip("Keep your distance ", "Always keep a two second gap between you and the car in front.")); 
    } 
} 

這是試圖訪問該列表的類。

public partial class tip : PhoneApplicationPage 
{ 
    public tip() 
    { 
     InitializeComponent(); 
     Random r = new Random(); 
     int rInt = r.Next(0, 6); 
     tipname.Text = Tips.tips[rInt].Name; 
     tipdesc.Text = Tips.tips[rInt].Description; 
    } 
} 

這是什麼原因引起的?有沒有更好的方法來存儲這些提示。我只需要在Windows電話頁面上輸出兩個文本塊的提示列表。

回答

6

它看起來並不像您曾經將自動實現的屬性tips初始化爲一個值。因此它是null並在您的靜態初始化器中導致異常。嘗試初始化值

static Tips() 
{ 
    tips = new List<Tip>(); 
    ... 
} 
3

提示尚未初始化。

你需要新的它。

static Tips() 
{ 
    tips = new List<Tip>(); 
    tips.Add(new Tip("Don't use your mobile phone whilst driving", "Making or receiving 
    ... 
} 
+0

確定「新」是一個動詞? :-) – svick 2013-03-07 01:25:58

+0

這完全合法:) – 2013-03-07 02:16:14

2
public static class Tips 
{ 
    //private List<Tip> roadtips = new List<Tip>(); 
    public static List<Tip> tips { get; set; } 

    static Tips() 
    { 
     tips = new List<Tip>(); 
     tips.Add(new Tip("Don't use your mobile phone whilst driving", "Making or receiving a call, even using a 'hands free' phone, can distract your attention from driving and could lead to an accident. ")); 
     tips.Add(new Tip("Children", "Children often act impulsively, take extra care outside schools, near buses and ice cream vans when they might be around.")); 
     tips.Add(new Tip("Take a break", "Tiredness is thought to be a major factor in more than 10% of road accidents. Plan to stop for at least a 15 minute break every 2 hours on a long journey.")); 
     tips.Add(new Tip("Don't drink and drive", "Any alcohol, even a small amount , can impair your driving so be a safe driver don't drive and drive.")); 
     tips.Add(new Tip("Anticipate ", "Observe and anticipate other road users and use your mirrors regularly.")); 
     tips.Add(new Tip("Use car seats ", "Child and baby seats should be fitted properly and checked every trip.")); 
     tips.Add(new Tip("Keep your distance ", "Always keep a two second gap between you and the car in front.")); 
    } 
} 
相關問題