C# System.TypeInitializationException:“提示”的类型初始值设定项引发异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15261332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:09:14  来源:igfitidea点击:

System.TypeInitializationException: The type initializer for 'Tips' threw an exception

c#listwindows-phone-7static-constructor

提问by Joel Dean

I am trying to access the values of a static list. However when I try to do so this exception is thrown.

我正在尝试访问静态列表的值。但是,当我尝试这样做时,会抛出此异常。

System.TypeInitializationException: The type initializer for 'Tips' threw an exception. -- -> System.NullReferenceException: Object reference not set to an instance of an object

System.TypeInitializationException:“提示”的类型初始值设定项引发异常。-- -> System.NullReferenceException: 未将对象引用设置为对象的实例

The class with the list.

带有列表的类。

    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."));
    }
}

This is class trying to access the list.

这是试图访问列表的类。

 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;
    }
}

What's causing this to occur? Is there a better way to store these tips. I just need a list of tips to output to two textblocks on a Windows phone page.

是什么导致这种情况发生?有没有更好的方法来存储这些提示。我只需要一个提示列表来输出到 Windows 手机页面上的两个文本块。

采纳答案by JaredPar

It doesn't look like you ever initialize the auto-implemented property tipsto a value. Hence it is nulland causing an exception in your static initializer. Try initializing the value

看起来您从未将自动实现的属性初始化tips为值。因此,它会null在您的静态初始值设定项中导致异常。尝试初始化值

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

回答by Marty Dill

tips hasn't been initialized.

提示尚未初始化。

You need to new it.

你需要新它。

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

回答by MethodMan

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."));
    }
}