title: MediaBrowserServiceCompat unable to bind to service
body: <p>My code is very simple, i just want to bind to the music service using MediaBrowserServiceCompat, actually i have a bigger program with the same problem so i tried to make a smaller one without all the unimportant parts and i get the following error:</p>
  
  <blockquote>
    <p>08-27 11:31:30.848 12214-12214/com.example.android.newcheck E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.android.newcheck, PID: 12214
                                                                                  java.lang.RuntimeException: Unable to bind to service com.example.android.newcheck.MusicStreamService@8c043ff with Intent { act=android.media.browse.MediaBrowserService cmp=com.example.android.newcheck/.MusicStreamService }: java.lang.NullPointerException: Attempt to invoke interface method 'android.os.IBinder android.support.v4.media.MediaBrowserServiceCompat$MediaBrowserServiceImpl.onBind(android.content.Intent)' on a null object reference
                                                                                      at android.app.ActivityThread.handleBindService(ActivityThread.java:3847)
                                                                                      at android.app.ActivityThread.access$2200(ActivityThread.java:221)
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1887)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:158)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                                   Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.os.IBinder android.support.v4.media.MediaBrowserServiceCompat$MediaBrowserServiceImpl.onBind(android.content.Intent)' on a null object reference
                                                                                      at android.support.v4.media.MediaBrowserServiceCompat.onBind(MediaBrowserServiceCompat.java:613)
                                                                                      at android.app.ActivityThread.handleBindService(ActivityThread.java:3834)
                                                                                      at android.app.ActivityThread.access$2200(ActivityThread.java:221) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1887) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                      at android.os.Looper.loop(Looper.java:158) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) </p>
  </blockquote>
  
  <p>i mainly used Ian Lake lectures on youtube and did everything he said but i don't know why i get this error, i try to find answers online but couldn't find any answer. 
  thanks for the help. </p>
  
  <p>AndroidManifest:</p>
  
  <pre><code>    &lt;service
          android:name=".MusicStreamService"
          android:exported="true"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.media.browse.MediaBrowserService" /&gt;
          &lt;/intent-filter&gt;
      &lt;/service&gt;
  </code></pre>
  
  <p>MainActivity:</p>
  
  <pre><code>    @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      MediaBrowserCompat mediaBrowser = new MediaBrowserCompat(MainActivity.this, new ComponentName(MainActivity.this, MusicStreamService.class),
              new MediaBrowserCompat.ConnectionCallback() {
                  @Override
                  public void onConnected() {
                      Log.v("MainActivity","connected");
                  }
                  @Override
                  public void onConnectionSuspended() {
                      super.onConnectionSuspended();
                  }
                  @Override
                  public void onConnectionFailed() {
                      super.onConnectionFailed();
                  }
              }, null);
      mediaBrowser.connect();
      Button click = (Button) findViewById(R.id.button);
      click.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              startService(new Intent(MainActivity.this, MusicStreamService.class));
          }
      });
  }
  </code></pre>
  
  <p>MusicStreamService:</p>
  
  <pre><code>public class MusicStreamService extends MediaBrowserServiceCompat {
  @Nullable
  @Override
  public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) {
      Log.v("***********", "getroot");
      return new BrowserRoot(getString(R.string.app_name), // Name visible in Android Auto
              null); // Bundle of optional extras
  }
  
  @Override
  public void onLoadChildren(@NonNull String parentId, @NonNull Result&lt;List&lt;MediaBrowserCompat.MediaItem&gt;&gt; result) {
      Log.v("*********", "one step before connect2");
      result.sendResult(null);
  }
  
  @Override
  public void onCreate() {
      MediaSessionCompat mediaSession = new MediaSessionCompat(this, MusicStreamService.class.getSimpleName());
      setSessionToken(mediaSession.getSessionToken());
      mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
              MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
      mediaSession.setActive(true);
  }
  </code></pre>