title: Android activity accessing service's static reference before the service is ready
body: <p>I am trying to fire a Text to speech service from an activity and the activity based on certain events would cause the appropriate string to be sent to the service which speaks out.</p>
  
  <p>Here is my TTSService:</p>
  
  <pre><code>public class TTSService extends Service implements TextToSpeech.OnInitListener{
  
      private String str;
      private TextToSpeech mTts;
      private static final String TAG="TTSService";
      public static TTSService sInstance;
  
  
  
      @Override
  
      public IBinder onBind(Intent arg0) {
  
          return null;
      }
  
  
      @Override
      public void onCreate() {
  
          mTts = new TextToSpeech(this,
                  this  // OnInitListener
          );
          mTts.setSpeechRate(0.5f);
          Log.v(TAG, "oncreate_service");
          str ="turn left please ";
          super.onCreate();
          sInstance=this;
      }
  
  
      @Override
      public void onDestroy() {
          // TODO Auto-generated method stub
          if (mTts != null) {
              mTts.stop();
              mTts.shutdown();
          }
          super.onDestroy();
      }
  
  
      @Override
      public void onInit(int status) {
          Log.v(TAG, "oninit");
          if (status == TextToSpeech.SUCCESS) {
              int result = mTts.setLanguage(Locale.US);
              if (result == TextToSpeech.LANG_MISSING_DATA ||
                      result == TextToSpeech.LANG_NOT_SUPPORTED) {
                  Log.v(TAG, "Language is not available.");
              } else {
  
                  sayHello(str);
  
              }
          } else {
              Log.v(TAG, "Could not initialize TextToSpeech.");
          }
      }
      public void sayHello(String str) {
          mTts.speak(str,
                  TextToSpeech.QUEUE_FLUSH,
                  null);
      }
  }
  </code></pre>
  
  <p>Here is my activity's code to create the service:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
           //rest of code.....
          Intent serviceIntent = new Intent();
          serviceIntent.setAction("com.packagename.texttospeech.TTSService");
          startService(serviceIntent);
  }
  </code></pre>
  
  <p>Here is the activity's onSensorChanged event handler:</p>
  
  <pre><code> @Override
      public void onSensorChanged(SensorEvent event) {
          float distance = event.values[0];
          while(TTSService.sInstance==null){
              try {
                  Thread.sleep(100);
              } catch (InterruptedException e) {
                  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
              }
          }
          TTSService.sInstance.sayHello(Float.valueOf(distance).toString());
  
      }
  </code></pre>
  
  <p>Now when I run the activity above, it hangs and does not respond.</p>
  
  <p>On removing the while loop above, I get the following error trace:</p>
  
  <pre><code>ERROR/AndroidRuntime(7469): FATAL EXCEPTION: main
          java.lang.NullPointerException
          at com.packagename.texttospeech.SensorTestActivity.onSensorChanged(SensorTestActivity.java:51)
          at android.hardware.SensorManager$ListenerDelegate$1.handleMessage(SensorManager.java:456)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:123)
          at android.app.ActivityThread.main(ActivityThread.java:4633)
          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:858)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
          at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>So clearly the problem is that in the line(<code>SensorTestActivity.java:51</code>):</p>
  
  <pre><code>TTSService.sInstance.sayHello(Float.valueOf(distance).toString());
  </code></pre>
  
  <p>Here the <code>sInstance</code> is null which suggests that TTSService's <code>onCreate()</code> is yet to be finished.So how should I make my activity wait till the <code>TTSService</code> is ready to be used?</p>