0

When I try to run the program, it throws me this error: Object reference not set to an instance of an object. Is there something wrong with the code? Help!

private void InitSpeechRecognition()
        {

            _audioSource = new KinectAudioSource
            {
                FeatureMode = true,
                AutomaticGainControl = false,
                SystemMode = SystemMode.OptibeamArrayOnly
            };
            var ri =
              SpeechRecognitionEngine.InstalledRecognizers().
                Where(r => r.Id == RecognizerId).FirstOrDefault();
            _engine = new SpeechRecognitionEngine(ri.Id);
            var gb = new GrammarBuilder { Culture = new CultureInfo("en-US") };


            gb.Append(CommandMessage.Choices);


            var g = new Grammar(gb);
            _engine.LoadGrammar(g);
            _engine.SpeechRecognized += SreSpeechRecognized;

            _audioStream = _audioSource.Start();
            _engine.SetInputToAudioStream(_audioStream,
                                          new SpeechAudioFormatInfo(
                                          EncodingFormat.Pcm, 16000, 16, 1,
                                          32000, 2, null));
            _engine.RecognizeAsync(RecognizeMode.Multiple);
        }

        private void SreSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {

            if (CommandMessage.Commands.ContainsKey(e.Result.Text))
            {
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal,
                  new Action(() =>
                    Messenger.Default.Send(
                       new CommandMessage { Command = CommandMessage.Commands[e.Result.Text] })));
            }
        }
6
  • 1
    can u provide stacktrace at the point of exception Commented May 17, 2012 at 9:15
  • 1
    Which line? Many of them can cause a NullReferenceException! ri can be null (because of FirstOrDefault), for example. Commented May 17, 2012 at 9:16
  • 1
    _engine = new SpeechRecognitionEngine(ri.Id); (Sorry, is this line!) Commented May 17, 2012 at 9:18
  • @Jess then the problem is that ri is null. You do not have any installed recognizer or simply there is not any recognizer with the given id. Commented May 17, 2012 at 9:19
  • 1
    Let the user to choose (via configuration, you'll show her the full list then you'll save the ID of the one she picked). As alternative (or if the user didn't choose anything or if you can't find the one she would like to use) pick the first one in the list. If the list is empty simply quit with an error. Tip: do not use FirstOrDefault() if the list can be empty and you do not handle the null case. Usually it's more easy to debug a more specific exception than NullReferenceException. Commented May 17, 2012 at 9:28

1 Answer 1

2
var ri = SpeechRecognitionEngine.InstalledRecognizers().
            Where(r => r.Id == RecognizerId).FirstOrDefault();

Here, FirstOrDefault() either returns the first object in the sequence or null if no match found. Then in the next line you are trying to access the Id property of the object, which may be null.

Perform a check for null after the assignment, and only use ri if it is not null. E.g.

  if (ri != null)
  {
    ...
  } 

Or use the First() method instead of FirstOrDefault() and surround the code with a try catch block to handle the case when the sequence is empty.

But the reason why you are getting null is probably where the real problem is. ri is null because there are no installed recognizers with the id of RecognizerId. I don't see the code where you are setting it, so look around that part.

Also, take a look at the example on this page, it might contain just what you need: http://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognitionengine.installedrecognizers.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.