GotSpeech.NET

The online community for Microsoft Speech Server developers
Welcome to GotSpeech.NET Sign in | Join | Help
in Search
CBrekeke Software

null refrence exception on adding dictationgrammar to recognizer

Last post 05-20-2010, 3:31 PM by michael.kerby. 2 replies.
Sort Posts: Previous Next
  •  05-19-2010, 1:44 PM 10424

    null refrence exception on adding dictationgrammar to recognizer

    I am new to the speech sdk.  I am just making a simple program to test things out and give a demo to some clients.  all i want to do is be able to speak into the microphone and have it show up in a text box.  I made a form with a single text box.  Here is the code running behind it.

     

    Imports System.Speech

    Imports System.Speech.Recognition

    Public Class Form1

    WithEvents reco As Recognition.SpeechRecognitionEngine

    Dim gram As New System.Speech.Recognition.DictationGrammar()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    gram = New Recognition.DictationGrammar

    reco.LoadGrammar(gram)

    reco.SetInputToDefaultAudioDevice()

    reco.RecognizeAsync()

    End Sub

     

    Private Sub reco_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.SpeechRecognizedEventArgs) Handles reco.SpeechRecognized

    TextBox1.Text = e.Result.Text

    End Sub

    End Class

     The program creates a null refrence exception when trying to load the grammar into the reco object.  I cant figure out why this is, as it seems to work fine for other people in examples i've seen on the net.  I'm running this in visual studio 2008 on a windows server 2008 box.  Any ideas?

  •  05-19-2010, 10:05 PM 10425 in reply to 10424

    Re: null refrence exception on adding dictationgrammar to recognizer

    Did you use the debugger to figure out which object is null?

    It would probably help if you detailed the error message.

     


    --
    How To Ask a Question: http://support.microsoft.com/kb/555375
    --
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at
    http://www.microsoft.com/info/cpyright.htm
  •  05-20-2010, 3:31 PM 10428 in reply to 10425

    Re: null refrence exception on adding dictationgrammar to recognizer

    actually, i figured that out.  I was missing the new on creating the recognizer.  I thought it had to do with the grammer.  feel like an idiot now.  Regardless, I still couldn't get it to work. I also added a list to grammar of user ids, and it does not put the correct one in the text box no matter what I say.  It always puts Todd in there, or doesn't do it at all.

    Imports System.Speech
    Imports System.Speech.Recognition
    Imports System.Speech.Recognition.SrgsGrammar

    Public Class Form1
        WithEvents reco As New Recognition.SpeechRecognitionEngine()
        Dim synth As New Synthesis.SpeechSynthesizer

        Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
            If txtUserID.Enabled = True Then
                txtUserID.Focus()
            End If
        End Sub

        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            synth.Speak("I quit!")
        End Sub

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            synth.SpeakAsync("Enter your user I.D. now.")
            txtUserID.Focus()


            Dim gram As New Recognition.SrgsGrammar.SrgsDocument
            Dim userRule As New Recognition.SrgsGrammar.SrgsRule("user")
            Dim usersList As New Recognition.SrgsGrammar.SrgsOneOf("Mike", "Vince", "Todd", "Gregg", "Ben")
            userRule.Add(usersList)
            gram.Rules.Add(userRule)
            gram.Root = userRule
            Dim rgram As New Grammar(gram)
            reco.LoadGrammar(rgram)
            Dim t As New Threading.Thread(AddressOf recoasync)
            t.Start()
        End Sub

        Private Sub reco_AudioSignalProblemOccurred(ByVal sender As Object, ByVal e As System.Speech.Recognition.AudioSignalProblemOccurredEventArgs) Handles reco.AudioSignalProblemOccurred
            Me.BackColor = Color.Red
        End Sub

        Private Sub reco_RecognizeCompleted(ByVal sender As Object, ByVal e As System.Speech.Recognition.RecognizeCompletedEventArgs) Handles reco.RecognizeCompleted
            reco.RecognizeAsync()
        End Sub

        Private Sub reco_SpeechDetected(ByVal sender As Object, ByVal e As System.Speech.Recognition.SpeechDetectedEventArgs) Handles reco.SpeechDetected
            Me.BackColor = Color.Aqua
        End Sub

        Private Sub reco_SpeechRecognitionRejected(ByVal sender As Object, ByVal e As System.Speech.Recognition.SpeechRecognitionRejectedEventArgs) Handles reco.SpeechRecognitionRejected
            synth.SpeakAsync("I don't understand.")
        End Sub


        Private Sub reco_SpeechRecognized(ByVal sender As Object, ByVal e As System.Speech.Recognition.SpeechRecognizedEventArgs) Handles reco.SpeechRecognized
            txtUserID.Text = txtUserID.Text & e.Result.Text
            'synth.SpeakAsync("You said " & e.Result.Text)
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If txtUserID.Enabled = True Then
                If txtUserID.Text = "" Then
                    synth.SpeakAsync("You need to enter a user I.D., morron.")
                    txtUserID.Focus()
                End If
            End If
        End Sub

        Private Function recoasync()
            reco.SetInputToDefaultAudioDevice()
            reco.RecognizeAsync()
        End Function
    End Class

View as RSS news feed in XML