-1

I'm creating windows form speech recolonization application but somehow not able to fix the object reference issue: Can anyone please tell where I'm going wrong, as I'm getting Getting Error object reference not set to an instance of the object in the below code.

public partial class Form1 : Form
    {
        SpeechSynthesizer ss = new SpeechSynthesizer();
        PromptBuilder pb = new PromptBuilder();
        SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
        Choices list;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            btnStop.Enabled = true;
            try
            {
                list.Add(new string[] { "Hello", "Open Chrome", "Close", "What is the current time", "Thank You" });
                Grammar gr = new Grammar(new GrammarBuilder(list));

                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception Caught");
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Exception Caught");
            }
        }
        void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            switch (e.Result.Text.ToString())
            {
                case "Hello":
                    ss.SpeakAsync("Hello There");
                    break;
                case "Open Chrome":
                    System.Diagnostics.Process.Start("Chrome", "https://www.google.com");
                    break;
                case "Close":
                    Application.Exit();
                    break;
                case "What is the current time":
                    ss.SpeakAsync("Current time is :" + DateTime.UtcNow.ToLongDateString());
                    break;
                case "Thank You":
                    ss.SpeakAsync("Thank You for using this service");
                    break;
                default:
                    ss.SpeakAsync("Please correct your choice");
                    break;
            }
            txtContents.Text = e.Result.Text.ToString() + Environment.NewLine;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            sre.RecognizeAsyncStop();
            btnStart.Enabled = true;
            btnStop.Enabled = false;
        }
    }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Bhuwan Pandey
  • 514
  • 1
  • 6
  • 19
  • It's not an error. It's an exception. All exceptions have names. In this case, `System.NullReferenceException`. This question is a huge duplicate, and will be closed as such. – John Saunders Jun 06 '15 at 06:45

1 Answers1

1

Instead of

Choices list;

do

Choices list = new Choices();

Reason: You use list even before it is initialized.

list.Add(new string[] { "Hello", "Open Chrome", "Close", "What is the current time", "Thank You" });
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208