• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

java.lang.NullPointerException when using Talktospeech objects

I am trying to create a mobile app, when the app is launched it should speak some words(Say for example Welome to this app... etc etc). but somehow i am unable to come up with a solution. Can someone please help me in this?

package com.example.helloworld;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.widget.Toast;

public class HelloAndroid extends Activity implements OnInitListener
{
TextToSpeech myTTS;
private static final int MY_DATA_CHECK_CODE = 1;


/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//check for TTS data
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);


}

@Override
protected void onStart() {
super.onStart();
// The activity is about to become visible.
String words = "Hi hi hi hi hi hi hi hi hi hi hi";
speakWords(words);

}

//speak the user text
private void speakWords(String speech) {
myTTS.setLanguage(Locale.US);

//speak straight away
myTTS.speak("Hello welcome to this app......", TextToSpeech.QUEUE_FLUSH, null);
}

//act on result of TTS data check
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
//the user has the necessary data - create the TTS
myTTS = new TextToSpeech(this, this);
}
else {
//no data - install it now
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
}
}
//setup TTS
public void onInit(int initStatus) {

//check for successful instantiation

}
}




Thanks in advance for your help
 
12-17 12:34:02.414: W/dalvikvm(392): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-17 12:34:02.424: E/AndroidRuntime(392): Uncaught handler: thread main exiting due to uncaught exception
12-17 12:34:02.454: E/AndroidRuntime(392): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.HelloAndroid}: java.lang.NullPointerException
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.os.Looper.loop(Looper.java:123)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-17 12:34:02.454: E/AndroidRuntime(392): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 12:34:02.454: E/AndroidRuntime(392): at java.lang.reflect.Method.invoke(Method.java:521)
12-17 12:34:02.454: E/AndroidRuntime(392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-17 12:34:02.454: E/AndroidRuntime(392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-17 12:34:02.454: E/AndroidRuntime(392): at dalvik.system.NativeStart.main(Native Method)
12-17 12:34:02.454: E/AndroidRuntime(392): Caused by: java.lang.NullPointerException
12-17 12:34:02.454: E/AndroidRuntime(392): at com.example.helloworld.HelloAndroid.speakWords(HelloAndroid.java:43)
12-17 12:34:02.454: E/AndroidRuntime(392): at com.example.helloworld.HelloAndroid.onStart(HelloAndroid.java:37)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.Activity.performStart(Activity.java:3723)
12-17 12:34:02.454: E/AndroidRuntime(392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
12-17 12:34:02.454: E/AndroidRuntime(392): ... 11 more
12-17 12:34:02.484: I/dalvikvm(392): threadid=7: reacting to signal 3
12-17 12:34:02.644: I/dalvikvm(392): Wrote stack trace to '/data/anr/traces.txt'
12-17 12:34:07.995: I/Process(392): Sending signal. PID: 392 SIG: 9
12-17 12:35:31.255: D/AndroidRuntime(418): Shutting down VM
12-17 12:35:31.255: W/dalvikvm(418): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-17 12:35:31.267: E/AndroidRuntime(418): Uncaught handler: thread main exiting due to uncaught exception
12-17 12:35:31.285: E/AndroidRuntime(418): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.HelloAndroid}: java.lang.NullPointerException
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.os.Looper.loop(Looper.java:123)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-17 12:35:31.285: E/AndroidRuntime(418): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 12:35:31.285: E/AndroidRuntime(418): at java.lang.reflect.Method.invoke(Method.java:521)
12-17 12:35:31.285: E/AndroidRuntime(418): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-17 12:35:31.285: E/AndroidRuntime(418): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-17 12:35:31.285: E/AndroidRuntime(418): at dalvik.system.NativeStart.main(Native Method)
12-17 12:35:31.285: E/AndroidRuntime(418): Caused by: java.lang.NullPointerException
12-17 12:35:31.285: E/AndroidRuntime(418): at com.example.helloworld.HelloAndroid.speakWords(HelloAndroid.java:43)
12-17 12:35:31.285: E/AndroidRuntime(418): at com.example.helloworld.HelloAndroid.onStart(HelloAndroid.java:37)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.Activity.performStart(Activity.java:3723)
12-17 12:35:31.285: E/AndroidRuntime(418): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
12-17 12:35:31.285: E/AndroidRuntime(418): ... 11 more
12-17 12:35:31.325: I/dalvikvm(418): threadid=7: reacting to signal 3
12-17 12:35:31.447: I/dalvikvm(418): Wrote stack trace to '/data/anr/traces.txt'
12-17 12:35:35.284: I/Process(418): Sending signal. PID: 418 SIG: 9
 
Back
Top Bottom