0

I am getting a null pointer exception report in the android developer console. I need some advise as to what possibly is the problem here, the stack trace is like this

java.lang.NullPointerException
at com.myfreeapp.workers.Speaker.onInit(Speaker.java:57)
at android.speech.tts.TextToSpeech$1.onServiceConnected(TextToSpeech.java:451)
at android.app.ActivityThread$PackageInfo$ServiceDispatcher.doConnected(ActivityThread.java:1247)
at android.app.ActivityThread$PackageInfo$ServiceDispatcher$RunConnection.run(ActivityThread.java:1264)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4668)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)

The relevant code snippet in my app is

public Speaker(final Context context, final Settings settings) 
{   
    this.settings = settings;
    params = new HashMap<String, String>();      
    params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "myfreeapps");

    tts= new TextToSpeech(context, this);   
    Utils.log(TAG, "Created TextToSpeech..");
}

@Override
public void onInit(final int status) 
{
    Utils.log(TAG, "TTS onInit..");

//below is line 57 mentioned in the stack trace
    tts.setOnUtteranceCompletedListener(new SpeechFinishedListener());
    tts.setLanguage(Locale.getDefault());
    tts.setSpeechRate(settings.getSpeed());
    tts.setPitch(settings.getPitch());      
    ready = true;

}

Please first of all I need to be clear what exactly is null.. Is the stack trace pointing to variable tts on line 57 to be null..?

Or is the null pointer exception happening inside the TextToSpeech method setOnUtteranceCompletedListener ?

The Speaker instance is created on the main thread in a sticky service, and when I debug my code the callback from TextToSpeech also comes back on the same thread..

I don't understand how could the variable tts be null ???

By the way this problem is not reproducible on my end. I have this stack strace reported several times on the developer console.

Please advise,

2
  • Is their a reason you are initializing your TextToSpeech object in the constructor versus inside onInit? Commented Mar 29, 2012 at 21:53
  • Well, onInit is called as a result of creating TextToSpeech(context, this); the second param to TextToSpeech constructor is the implementaion of OnInitListener which has the method onInit.. Speech class is implementing OnInitListener interface.. Commented Mar 30, 2012 at 1:37

4 Answers 4

2

Your code should work, but I'd try this to see if it fixes the problem:

at the top of your code put this:

Handler handler = new Handler()

then make your other code have this:

handler.post(new Runnable()
    {
        @Override
        public void run()
        {
            tts.setOnUtteranceCompletedListener(new SpeechFinishedListener());
            tts.setLanguage(Locale.getDefault());
            tts.setSpeechRate(settings.getSpeed());
            tts.setPitch(settings.getPitch());                    
        }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I would try it, also I would like to understand this piece. Is it something like posting on the UI thread que to be called later ? Like invokeLater of swing ??
1

I resolved the problem, here is how it worked

There was a case where I was creating Speaker object on a non-ui thread. The callback to onInit() would come on UI thread/main thread..

now even though running it might not always crash but I realised it was not safe and chances of null pointer exceptions are there..

so my updated code looks like this

public Speaker(final Context context, final Settings settings) 
{   
    this.settings = settings;
    params = new HashMap<String, String>();      
    params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
    params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "myfreeapps");

    synchronize(synchobject)
    {
       tts= new TextToSpeech(context, this);   
    }
    Utils.log(TAG, "Created TextToSpeech..");
}

@Override
public void onInit(final int status) 
{
    Utils.log(TAG, "TTS onInit..");

    synchronize(synchobject)
    {

    tts.setOnUtteranceCompletedListener(new SpeechFinishedListener());
    tts.setLanguage(Locale.getDefault());
    tts.setSpeechRate(settings.getSpeed());
    tts.setPitch(settings.getPitch());      
    ready = true;
    }

}

This seems to have resolve my problem , thanks every one for your contribution to help me out,..

Comments

0

Probably, the TextToSpeech engine is not installed on the device.

1 Comment

I investigated code of TextToSpeech the third line of the tack trace,. at android.speech.tts.TextToSpeech$1.onServiceConnected(TextToSpeech.java:451) . The onInit is being called with SUCCESS code
0

Am I correct in assuming that Speaker is a class that is being called from outside your main activity?

It seems as if the onInit method is being called before the constructor. I see you have checked for this with log tags; what was your result?

2 Comments

Speaker instance is created in a Service's onStartCommand(..).I only have stack trace available from the android developer console. I thought it was not possible that onInit() could ever be called before constructor ??
I was of the same impression that onInit should never be called before the constructor. In troubleshooting it never hurtss to double-check the implementation tho.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.