title: Android: Playback State on Media Browser Client returns null pointer exception
body: <p>I'm currently developing a simple media player using a client/service approach using MediaBrowser compat from the Android support library.</p>
  
  <p>I'm getting the following exception:</p>
  
  <pre><code>FATAL EXCEPTION: main
  Process: valdivia.peter.inclubooks, PID: 32383
  java.lang.NullPointerException: Attempt to invoke virtual method 'int android.support.v4.media.session.PlaybackStateCompat.getState()' on a null object reference
  at valdivia.peter.inclubooks.activities.MostrarAudiolibroActivity$3.onClick(MostrarAudiolibroActivity.java:118)
  at android.view.View.performClick(View.java:5201)
  at android.view.View$PerformClick.run(View.java:at android.os.Handler.handleCallback(Handler.java:
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5443)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  </code></pre>
  
  <p>What I understand is that either the controller or the playbackstate are null, so obviously it can't return the int representing the player's state.</p>
  
  <p>Here are how I create the controller on the client activity in the onConnected() method from the MediaBrowser callback:</p>
  
  <pre><code>@Override
          public void onConnected() {
              super.onConnected();
              Log.d(tag,"Conecté al servicio");
              Log.d(tag,"token:" + mMediaBrowser.getSessionToken().toString());
              try{
                  controller = new MediaControllerCompat(getApplicationContext(),mMediaBrowser.getSessionToken());
              }catch(RemoteException exception){
                  Log.d(tag,"Remote Exception al crear el Media Controller");
                  Toast.makeText(getApplicationContext(),"Error al crear los controles de la sesión",Toast.LENGTH_LONG).show();
                  return;
              }
              controller.registerCallback(mControllerCallback);
              MediaControllerCompat.setMediaController(MostrarAudiolibroActivity.this,controller);
              buildTransportControls();
          }
  </code></pre>
  
  <p>And here how I build the transport controls:</p>
  
  <pre><code>private void buildTransportControls() {
      ImageButton mBotonPausa = (ImageButton) findViewById(R.id.mostrar_audiolibro_playpause);
      mBotonPausa.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              MediaControllerCompat mediaControllerCompat = MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this);
              int estado = mediaControllerCompat.getPlaybackState().getState();
              if(estado == PlaybackStateCompat.STATE_PLAYING){
                  MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().pause();
              }else if (estado == PlaybackStateCompat.STATE_PAUSED){
                  MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().play();
              }else{
                  Bundle extras = new Bundle();
                  extras.putString("titulo",MostrarAudiolibroActivity.this.audiolibro.getTitulo());
                  extras.putString("autor",MostrarAudiolibroActivity.this.audiolibro.getAutor());
                  MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().playFromUri(Uri.parse(MostrarAudiolibroActivity.this.audiolibro.getLinkAudiolibro()),extras);
                  Log.d(tag,"Voy a tratar de abrir el audio");
              }
          }
      });
      ImageButton mBotonReiniciar = (ImageButton) findViewById(R.id.mostrar_audiolibro_reiniciar);
      mBotonReiniciar.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().seekTo(0);
              //MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().play();
          }
      });
  
  
  }
  </code></pre>
  
  <p>What I'm doing wrong. Thanks in advance.</p>