简体   繁体   English

System.NullReferenceException - 未将对象引用设置为对象的实例

[英]System.NullReferenceException – Object reference not set to an instance of an object

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] })));
            }
        }
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. 这里,FirstOrDefault()返回序列中的第一个对象,如果找不到匹配则返回null。 Then in the next line you are trying to access the Id property of the object, which may be null. 然后在下一行中,您尝试访问对象的Id属性,该属性可能为null。

Perform a check for null after the assignment, and only use ri if it is not null. 在赋值后执行null检查,如果它不为null,则仅使用ri。 Eg 例如

  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. 或者使用First()方法代替FirstOrDefault()并使用try catch块包围代码以处理序列为空时的情况。

But the reason why you are getting null is probably where the real problem is. 但是你得到null的原因可能就是真正的问题所在。 ri is null because there are no installed recognizers with the id of RecognizerId. ri为null,因为没有安装ID为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 此外,请查看此页面上的示例,它可能只包含您需要的内容: http//msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognitionengine.installedrecognizers.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Foreach System.NullReferenceException:未将对象引用设置为对象的实例 - Foreach System.NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException:对象引用未设置为对象的实例 - System.NullReferenceException: Object reference not set to an instance of an object 类型'System.NullReferenceException'的异常:对象引用未设置为对象的实例 - An exception of type 'System.NullReferenceException' : Object reference not set to an instance of an object 错误:System.NullReferenceException:对象引用未设置为对象的实例 - Error :System.NullReferenceException: Object reference not set to an instance of an object Xamarin android System.NullReferenceException:未将对象引用设置为对象的实例 - Xamarin android System.NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException:未将对象引用设置为对象的实例 - System.NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException:对象引用未设置为对象的实例 - System.NullReferenceException: Object reference not set to an instance of an object System.NullReferenceException:“对象引用未设置为 object 的实例。” 问题 - System.NullReferenceException: „Object reference not set to an instance of an object.” problem 如何修复 System.NullReferenceException:Object 引用未设置为 object 的实例 - How to fix System.NullReferenceException: Object reference not set to an instance of an object 会话{“对象引用未设置为对象的实例。”} System.Exception {System.NullReferenceException} - session {“Object reference not set to an instance of an object.”} System.Exception {System.NullReferenceException}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM