类型为“System.NullReferenceException”错误的未处理异常

时间:2015-07-27 14:34:09

标签: c# compiler-errors nullreferenceexception

执行代码时出现以下错误

  

SimpleProgram.exe中出现未处理的“System.NullReferenceException”类型异常“

我已经研究了很多但是找不到我认为适用于我的任何东西...... 我认为错误发生在这里:

clist.Add(new string[] { "hello", "what is the time", "open chrome", "close" });

但这里是完整的代码以防万一:

private void button1_Click(object sender, EventArgs e)
{
    //START CLICKED
    btnStart.Enabled = false;
    btnStop.Enabled = true;
    clist.Add(new string[] { "hello", "what is the time", "open chrome", "close" });
    Grammar gr = new Grammar(new GrammarBuilder(clist));

    try
    {
        sre.RequestRecognizerUpdate();
        sre.LoadGrammar(gr);
        sre.SpeechRecognized += Sre_SpeechRecognized;
        sre.SetInputToDefaultAudioDevice();
        sre.RecognizeAsync(RecognizeMode.Multiple);

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "error: Words not recognized!");
    }
}

private void Sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    switch( e.Result.Text.ToString())
    {
        case "hello":
            ss.SpeakAsync("Hello Andrew");
            break;
        case "what is the time":
            ss.SpeakAsync("It's" + DateTime.Now.ToLongTimeString());
            break;
        case "open chrome":
            System.Diagnostics.Process.Start("chrome", "http://www.google.co.uk");
            break;
        case "close":
            Application.Exit();
            break;

    }
    txtContents.Text += e.Result.Text.ToString() + Environment.NewLine;
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void btnStop_Click(object sender, EventArgs e)
{
    sre.RecognizeAsyncStop();
    btnStart.Enabled = true;
    btnStop.Enabled = false;
}

0 个答案:

没有答案