4

静的リストの値にアクセスしようとしています。ただし、そうしようとすると、この例外がスローされます。

System.TypeInitializationException:「Tips」の型初期化子が例外をスローしました。---> 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;
    }
}

これが発生する原因は何ですか?これらのヒントを保存するためのより良い方法はありますか?WindowsPhoneページの2つのテキストブロックに出力するためのヒントのリストが必要です。

4

3 に答える 3

8

自動実装されたプロパティを値に初期化したようには見えませんtips。したがってnull、静的初期化子で例外が発生します。値を初期化してみてください

static Tips()
{
  tips = new List<Tip>();
  ...
}
于 2013-03-07T01:20:15.733 に答える
3

ヒントが初期化されていません。

あなたはそれを新しくする必要があります。

 static Tips()
 {
    tips = new List<Tip>();
    tips.Add(new Tip("Don't use your mobile phone whilst driving", "Making or receiving 
    ...  
 }
于 2013-03-07T01:20:22.867 に答える
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."));
    }
}
于 2013-03-07T01:21:59.260 に答える