pocketsphinx NullPointerException in onBeginningOfSpeech() but the object is not null

burritoburrito Member ✭✭

I'm using the below code to start listening on my device for voice commands but as soon as I call SpeechRecognizer.StartListening() I get a NullPointerException.

Here is the full error:

Java.Lang.NullPointerException: Attempt to invoke interface method 'void edu.cmu.pocketsphinx.RecognitionListener.onBeginningOfSpeech()' on a null object reference

as you can see in the code below, I'm checking to make sure my object reference is NOT null. Can anyone shed some light here?

    using System;
    using System.Threading.Tasks;
    using Edu.Cmu.Pocketsphinx;


    namespace ExpeditionSpotMobile.Droid.Utilities
    {
        public class VoiceRecognizer : Java.Lang.Object,  IRecognitionListener
        {
            private SpeechRecognizer speechRecognizer;
            public IntPtr Handle
            {
                get; set;
            }
            MainActivity _mainActivity;

            public VoiceRecognizer()
            {

            }

            public VoiceRecognizer(MainActivity mainActivity)
            {
                _mainActivity = mainActivity;
                InitializeVoiceRecognizer();
            }

            private async Task InitializeVoiceRecognizer()
            {
                await SetupRecognizer();
                SwitchSearch("wakeup");
            }
            private async Task SetupRecognizer()
            {
                Java.Lang.JavaSystem.LoadLibrary("pocketsphinx_jni");
                var config = Edu.Cmu.Pocketsphinx.Decoder.DefaultConfig();
                Java.IO.File filePath = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/MyApp/voice_recognition");
                speechRecognizer = SpeechRecognizerSetup
                    .DefaultSetup()
                    .SetAcousticModel(new Java.IO.File(filePath, "en-us-ptm"))
                    .SetDictionary(new Java.IO.File(filePath, "cmudict-en-us.dict"))
                    .SetKeywordThreshold(1e-45f)
                    .SetBoolean("-allphone_ci", true)
                    .Recognizer;
                speechRecognizer.AddListener(this);
                speechRecognizer.AddKeywordSearch("wakeup", new Java.IO.File(filePath, "commands.lst"));            
            }
            private void SwitchSearch(string searchName)
            {
                speechRecognizer.Stop();
                if (searchName == "wakeup")
                {
                    if (speechRecognizer != null)
                        speechRecognizer.StartListening(searchName);
                }
            }

            public void Dispose()
            {
                if (speechRecognizer != null)
                {
                    speechRecognizer.Cancel();
                    speechRecognizer.Shutdown();
                }
            }

            public void OnBeginningOfSpeech()
            {

            }

            public void OnEndOfSpeech()
            {
                if (speechRecognizer.SearchName != "wakeup")
                    SwitchSearch("wakeup");
            }

            public void Stop()
            {
                speechRecognizer.Stop();
            }

            public void OnError(Java.Lang.Exception p0)
            {
                //throw new NotImplementedException();
            }

            public void OnPartialResult(Hypothesis hypothesis)
            {
                if (hypothesis == null)
                    return;
                SwitchSearch("wakeup");
            }



            public void OnResult(Hypothesis hypothesis)
            {
                if (hypothesis != null)
                {
                    string text = hypothesis.Hypstr;
                    // do something here
                }
            }

            public void OnTimeout()
            {
                SwitchSearch("wakeup");
            }
        }
    }

Best Answer

  • burritoburrito ✭✭
    Accepted Answer

    got this sorted out by removing the Handle variable and Dispose() method that VS automatically added when I implemented the interface.

Answers

  • burritoburrito Member ✭✭
    Accepted Answer

    got this sorted out by removing the Handle variable and Dispose() method that VS automatically added when I implemented the interface.

Sign In or Register to comment.