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.