1

I am implementing voice recognition in my service, but am getting a null pointer exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'void edu.cmu.pocketsphinx.SpeechRecognizer.addListener(edu.cmu.pocketsphinx.RecognitionListener)' on a null object reference

(Full error posted below)

Here is the relevant portion of my service:

public class MyService extends Service implements RecognitionListener {
    SpeechRecognizer mSpeechRecognizer;  
    public MyService() {

    }    
    @Override
    public void onCreate() {
        super.onCreate();    
        try {
            Assets assets = new Assets(MyService.this); //HERE IS WHERE THE ERROR OCCURS
            File assetDir = assets.syncAssets();
            setupRecognizer(assetDir);    
            if (mSpeechRecognizer != null) {
                mSpeechRecognizer.cancel();
            }
            mSpeechRecognizer.startListening("KEYWORD");
            Log.v(TAG, "Started Listening");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setupRecognizer(File sphinxDir) {
        try {
            mSpeechRecognizer = defaultSetup()
                    .setAcousticModel(new File(sphinxDir, "en-us-ptm"))
                    .setDictionary(new File(sphinxDir, "cmudict-en-us.dict"))
                    .setBoolean("-allphone_ci", true)
                    .setKeywordThreshold(1e-40f)
                    .getRecognizer();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mSpeechRecognizer.addListener(this);
        mSpeechRecognizer.addKeyphraseSearch("KEYWORD", "Oh Mighty Computer");
    }

Here is the full error

FATAL EXCEPTION: main Process: com.example.ruchirb.appName, PID: 1520 java.lang.RuntimeException: Unable to create service com.example.ruchirb.appName.MyService: java.lang.NullPointerException: Attempt to invoke virtual method 'void edu.cmu.pocketsphinx.SpeechRecognizer.addListener(edu.cmu.pocketsphinx.RecognitionListener)' on a null object reference at android.app.ActivityThread.handleCreateService(ActivityThread.java:2905) at android.app.ActivityThread.access$1900(ActivityThread.java:157) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1439) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5527) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void edu.cmu.pocketsphinx.SpeechRecognizer.addListener(edu.cmu.pocketsphinx.RecognitionListener)' on a null object reference at com.example.ruchirb.appName.MyService.setupRecognizer(MyService.java:71) at com.example.ruchirb.appName.MyService.onCreate(MyService.java:40) at android.app.ActivityThread.handleCreateService(ActivityThread.java:2895) at android.app.ActivityThread.access$1900(ActivityThread.java:157)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1439)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5527)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

Why am I getting this error? How can I resolve it?


EDIT:

I am baffled because the logcat reveals that my SpeechRecognizer object is null, stating that I am invoking addListener(edu.cmu.pocketsphinx.RecognitionListener) on a null object, but I do initialize my recognizer in my #setUpRecognizer() method.

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • 1
    You basically have the same question of "What is a NullPointerException, and how do I fix it"... What have you tried to do to solve the problem? The error in the logcat points at the line causing the problem – OneCricketeer Apr 24 '16 at 20:13
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – OneCricketeer Apr 24 '16 at 20:14
  • 1
    @cricket_007 No, because I initialize the recognizer in the `setupRecognizer()` method, which is why it's odd. – Ruchir Baronia Apr 24 '16 at 20:14
  • @cricket_007 Thanks for pointing out that concern though, I'll edit my question to address it. :) – Ruchir Baronia Apr 24 '16 at 20:15
  • 1
    You try to, sure, but `mSpeechRecognizer.addListener(this);` is going to throw that error if any other exception is caught in the creation of it – OneCricketeer Apr 24 '16 at 20:17
  • If you move the catch statement down a few lines, then what do you see in the logcat? – OneCricketeer Apr 24 '16 at 20:22
  • @cricket_007 What do you mean down a few lines? It is already at the bottom of the method. Thanks! :) – Ruchir Baronia Apr 24 '16 at 20:38
  • 2
    The catch statement in `setupRecognizer` is not at the end of the method. You are getting your mentioned error because you caught some exception during the initialization of the recognizer, and the code continued on – OneCricketeer Apr 24 '16 at 20:41
  • @cricket_007 Oh, I thought you were talking about my first try-catch. So I should move `mSpeechRecognizer.addListener(this); mSpeechRecognizer.addKeyphraseSearch("KEYWORD", "Oh Mighty Computer");` in the try? – Ruchir Baronia Apr 24 '16 at 20:42
  • 1
    Yes, please. That will prevent the app from crashing and show you the first exception message – OneCricketeer Apr 24 '16 at 20:44
  • @cricket_007 I tried, but it is still crashing. I'm not sure why that would stop it from crashing in the first place... – Ruchir Baronia Apr 24 '16 at 20:59
  • @cricket_007 But you're right, I am getting the IOException from the catch – Ruchir Baronia Apr 24 '16 at 21:02
  • @cricket_007 Failed to initialize recorder. Microphone might be already in use. – Ruchir Baronia Apr 24 '16 at 21:02
  • @cricket_007 However, I do not have any apps open that use my microphone... – Ruchir Baronia Apr 24 '16 at 21:03
  • 1
    Catching a RuntimeException would prevent the app from crashing. If it still crashes with the code inside the catch, then your `getRecognizer()` is returning null and you should read the API documentation for pocketsphinx as why that'll happen – OneCricketeer Apr 24 '16 at 21:04
  • Did you read the tutorial documentation? Add the correct permissions to your app manifest? Try to download and run some example code? http://cmusphinx.sourceforge.net/wiki/tutorialandroid – OneCricketeer Apr 24 '16 at 21:06
  • 1
    you mentioned that failed to initialize recorder, did you have the correct permission in the manifest. The other thing to check, reboot the handset, clean start up, run the app again, does it crash, if it does, then there's something in your set up that is not correct. Also, why are you adding in the listener *after* the catch block when its null, refactor it or include it within the try block. – t0mm13b Apr 24 '16 at 21:35

0 Answers0