title: Xamarin android - Attempt to invoke virtual method sendBroadcast() on a null object reference
body: <p>I'm building a crossplatform application using Xamarin,my application will download videos from internet, I've done the download and save functions, but after saved videos to DCIM(in android) the videos do not appear in gallery.After hafl of a day searching for solutions on internet I comeup with two solutions:</p>
  
  <p>1.after download finished: send broadcast with action "Intent.ActionMediaMounted".</p>
  
  <p>2.after download finished Call "MediaScannerConnection.ScanFile".</p>
  
  <p>With solution 1 I got:</p>
  
  <p>"{Java.Lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.sendBroadcast(android.content.Intent)' on a null object reference}</p>
  
  <p>With solution 2, I got:</p>
  
  <p>"{Java.Lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Context.bindService(android.content.Intent, android.content.ServiceConnection, int)' on a null object reference}</p>
  
  <p>This is my Interface which I used to handle downloadfinished placed in PLC project:</p>
  
  <pre><code>namespace VideoDownloader
      {
        public interface IDownloadState
        {
            void OnDownloadStarted();
            void OnDownloadError();
            void OnDownloadFinished(string path);
         }
      }
  </code></pre>
  
  <p>This is my MainPage.xaml.cs in PLC project:</p>
  
  <pre><code>namespace VideoDownloader
  {
      public partial class MainPage : TabbedPage
      {
  
          public MainPage()
          {
              InitializeComponent();
          }
  
          public void downloadVideo(VideoInfor video, bool isRetried)
          {
            //download code here...                                    
            DependencyService.Get&lt;IDownloadState()
            .OnDownloadFinished(video.path);
           }
      }
  
  }
  </code></pre>
  
  <p>And This is my MainActivity in Android project which implement IDownloadState and send broadcast when OnDownloadFinished fired:</p>
  
  <pre><code>[assembly: Xamarin.Forms.Dependency(typeof(MainActivity))]
  namespace VideoDownloader.Droid
  {
      [Activity(Label = "VideoDownloader", Icon = "@mipmap/icon", Theme = 
  "@style/MainTheme", MainLauncher = true, ConfigurationChanges = 
  ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
      public class MainActivity : 
  global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, 
  IDownloadState
      {
          readonly string[] StoragePermissions =
          {
              Manifest.Permission.WriteExternalStorage,
              Manifest.Permission.ReadExternalStorage
          };
  
          const int RequestStorageId = 0;
  
          protected override void OnCreate(Bundle bundle)
          {
              TabLayoutResource = Resource.Layout.Tabbar;
              ToolbarResource = Resource.Layout.Toolbar;
  
              base.OnCreate(bundle);
  
              global::Xamarin.Forms.Forms.Init(this, bundle);
              LoadApplication(new App());
              int sdkBuildVersion = (int)Build.VERSION.SdkInt;
              Console.WriteLine("SDK build version = " + sdkBuildVersion);
              if (sdkBuildVersion &gt;= 23)
              {
                  RequestStoragePermission();
              }
          }
  
      public override void OnRequestPermissionsResult(int requestCode, 
                      string[] permissions, Permission[] grantResults)
          {
              switch (requestCode)
              {
                  case RequestStorageId:
                      {
                          if (grantResults[0] == Permission.Granted)
                          {
  
                          }
                          else
                          {
  
                          }
                      }
                      break;
                  default:
                      break;
              }
          }
  
  
          public void OnDownloadStarted()
          {
              throw new NotImplementedException();
          }
  
          public void OnDownloadError()
          {
              throw new NotImplementedException();
          }
  
          public void OnDownloadFinished(string path)
          {
              //Intent intent = new Intent(Intent.ActionMediaMounted);
              //SendBroadcast(intent);
              //throw new NotImplementedException();
              MediaScannerConnection.ScanFile(this, new String[] { 
                Android.OS.Environment
  .GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim)
  .AbsolutePath }, null, null);
          }
      }
  }
  </code></pre>
  
  <p>I know there are many many similar questions for android on stackoverflow that have ansered but they seem to not like my situation at all. So is there anyone know what am I doing wrong? Why this is happening?</p>