title: Null pointer exception - Service SMS with Text To Speech
body: <p>I want to make an application with can say a text with a TextToSpeach when I receive a SMS.</p>
  
  <p>For that I have a Service, a Receiver and a class SaidThis. </p>
  
  <p>See my code :</p>
  
  <p>My service :</p>
  
  <pre><code>package fr.dabernat.smsreceiver;
  
  import android.app.IntentService;
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.Handler;
  
  public class Service extends IntentService {
  
  
      private Handler handler;
      public static String text;
  
      public Service() {
          super("Service");
      }
  
      @Override
      public void onCreate() {
          super.onCreate();
          handler = new Handler();
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
      }
  
      @Override
      protected void onHandleIntent(Intent intent) {
          Bundle extras = intent.getExtras();
  
          if (!extras.isEmpty()) {
  
              SaidThis test = new SaidThis();
              test.SpeakText();
              //showNotification();
              //SaidThis test = new SaidThis(this);
              //test.text("J'ai honte de moi");
  
  
  
          }
  
          Receiver.completeWakefulIntent(intent);
      }
  
  }
  </code></pre>
  
  <p>My Receiver :</p>
  
  <pre><code>package fr.dabernat.smsreceiver;
  
  import android.app.Activity;
  import android.content.ComponentName;
  import android.content.Context;
  import android.content.Intent;
  import android.support.v4.content.WakefulBroadcastReceiver;
  
  
  public class Receiver extends WakefulBroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
  
          // Explicitly specify that Service will handle the intent.
          ComponentName comp = new ComponentName(context.getPackageName(),
          Service.class.getName());
  
          // Start the service, keeping the device awake while it is launching.
          startWakefulService(context, (intent.setComponent(comp)));
          setResultCode(Activity.RESULT_OK);
      }
  
  
  }
  </code></pre>
  
  <p>My class SaidThis : </p>
  
  <pre><code>package fr.dabernat.smsreceiver;
  import android.speech.tts.TextToSpeech;
  
  import java.util.Locale;
  
  /**
   * Created by Damien on 19/09/2014.
   */
  public class SaidThis extends Service implements TextToSpeech.OnInitListener, TextToSpeech.OnUtteranceCompletedListener {
  
      TextToSpeech tts;
  
      @Override
      public void onInit(int status) {
          if (status == TextToSpeech.SUCCESS) {
             tts.setLanguage(Locale.FRANCE);
  
          }
      }
  
      @Override
      public void onUtteranceCompleted(String s) {
          stopSelf();
      }
  
      public void SpeakText(){
          tts.speak(text,TextToSpeech.QUEUE_FLUSH, null);
      }
  }
  </code></pre>
  
  <p>My Android Manifest : </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="fr.dabernat.smsreceiver" &gt;
  
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
      &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
      &lt;uses-permission android:name="android.permission.RECEIVE_SMS" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
  
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;receiver
              android:name=".Receiver" &gt;
              &lt;intent-filter&gt;
                  &lt;!-- A CHANGER POUR "ECOUTER" UN AUTRE BROADCAST --&gt;
                  &lt;action android:name="android.provider.Telephony.SMS_RECEIVED" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  
          &lt;service android:name=".Service" /&gt;
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Errors :</p>
  
  <pre><code>10-08 10:44:51.980    1371-1371/fr.dabernat.smsreceiver E/dalvikvm﹕ &gt;&gt;&gt;&gt;&gt; Normal User
  10-08 10:44:51.980    1371-1371/fr.dabernat.smsreceiver E/dalvikvm﹕ &gt;&gt;&gt;&gt;&gt; fr.dabernat.smsreceiver [ userId:0 | appId:10309 ]
  10-08 10:44:52.080    1371-1412/fr.dabernat.smsreceiver E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[Service]
      Process: fr.dabernat.smsreceiver, PID: 1371
      java.lang.NullPointerException
              at fr.dabernat.smsreceiver.SaidThis.SpeakText(SaidThis.java:34)
              at fr.dabernat.smsreceiver.Service.onHandleIntent(Service.java:36)
              at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:157)
              at android.os.HandlerThread.run(HandlerThread.java:61)
  </code></pre>
  
  <p>I think the problem come from my service but I don't know where.
  I try to find some answer on the web before but I don't find any solution to my problem.</p>