Home » Android » android – TextToSpeech show Null Pointer Exception?

android – TextToSpeech show Null Pointer Exception?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am creating one application on TextToSpeech. But when i am trying to run,it throw exception on following line.

tts.speak(ruleOne, TextToSpeech.QUEUE_ADD, null);

Here is my full code for reference
Please give me any hint. I don’t understand what wrong in my code. when i compile my code all value pass correctly but when it jump on above tts object it hit null pointer exception. is any syntax is wrong? which method call first onCreate() or onActivityResult?
Thanks in Advance

Here is Logcat content.

04-10 13:58:34.082: WARN/System.err(19352): java.lang.NullPointerException
04-10 13:58:34.082: WARN/System.err(19352):     at com.example.examguide.ExamRulesActivity.onCreate(ExamRulesActivity.java:60)
04-10 13:58:34.082: WARN/System.err(19352):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-10 13:58:34.092: WARN/System.err(19352):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-10 13:58:34.092: WARN/System.err(19352):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-10 13:58:34.092: WARN/System.err(19352):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-10 13:58:34.092: WARN/System.err(19352):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-10 13:58:34.092: WARN/System.err(19352):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 13:58:34.092: WARN/System.err(19352):     at android.os.Looper.loop(Looper.java:123)
04-10 13:58:34.092: WARN/System.err(19352):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-10 13:58:34.102: WARN/System.err(19352):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 13:58:34.102: WARN/System.err(19352):     at java.lang.reflect.Method.invoke(Method.java:507)
04-10 13:58:34.102: WARN/System.err(19352):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-10 13:58:34.102: WARN/System.err(19352):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-10 13:58:34.102: WARN/System.err(19352):     at dalvik.system.NativeStart.main(Native Method)
How to&Answers:

write

tts = new TextToSpeech(this, this);

in your if loop in onCreate () method.

Answer:

As Dhruvisha and andy boot suggested, the NullPointerException is obvious because onCreate is executed before onActivityResult. My suggestion is to bring all your code that use tts to a procedure after the check. Here is my suggest code:

    @Override
    public void onCreate(Bundle me)
    {
        super.onCreate(me);
        setContentView(R.layout.examrules);
               Intent checkIntent = new Intent();
          checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
          startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // success, create the TTS instance
                tts = new TextToSpeech(this, this);
                this.speak();
            } 
            else {
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);

               //tts.isLanguageAvailable(Locale.INDIA_HINDI);
                startActivity(installIntent);

            }
        }

    }

    @Override
    public void onInit(int status) 
    {        
        if (status == TextToSpeech.SUCCESS) 
        {
            // tts.setLanguage(Locale.US);

            Locale loc = new Locale ("IN", "en");
            tts.setLanguage(loc);
            Toast.makeText(ExamRulesActivity.this, 
                    "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
        }
        else if (status == TextToSpeech.ERROR) 
        {
            Toast.makeText(ExamRulesActivity.this, 
                    "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
        }
    }

    public void speak() {
        int isVoiceEnabled=bundle.getInt("isVoiceEnabled");
        setResult(RESULT_OK, intent);       
        if(isVoiceEnabled==1)
        {
             String ruleOne="hi.";
             String ruleTwo= "How are you"; 
             String ruleThree= "will you meet e=me?"; 
             String ruleFour=  " No,Ok";
                     if (ruleOne!=null && ruleOne.length()>0) 
            {
             tts.speak(ruleOne, TextToSpeech.QUEUE_ADD, null);
             tts.speak(ruleTwo, TextToSpeech.QUEUE_ADD, null);
                 tts.speak(ruleThree, TextToSpeech.QUEUE_ADD, null);
             tts.speak(ruleFour, TextToSpeech.QUEUE_ADD, null);

              }       
        }
    }

}

Answer:

Isn’t onCreate run before onActivityResult?

Therefore your tts object will be null. Add extra code to check tts is not null before you call tts.speak

Answer:

You just need to copy this lines

Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);

And Write then right after you wrote setContentView()

That Because before doing anything with the TextToSpeach object You need to check that data is installed or not.

So the mistake in the code is that you wrote everything of new in that example you wrote it before the intent call.