title: Attempt to invoke virtual method 'java.lang.String java.io.File.getCanonicalPath()' on a null object reference
body: <p>I'm setting up a share button on my app, that takes a screenshot of a particular list view, and then lets the user to share this. To do this, I created three methods:</p>
  
  <ol>
  <li>Take the screenshot</li>
  <li>Save it in my storage</li>
  <li>Share it to the user.</li>
  </ol>
  
  <p>Here is my code:</p>
  
  <p><strong>Android Manifest file:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" 
  
  encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.transfergame"&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
      &lt;activity android:name=".ViewPlayerHistoryContents" /&gt;
      &lt;activity android:name=".CorrectScreen" /&gt;
      &lt;activity
          android:name=".DifficultyScreen"
          android:label="Transfer Game"&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;provider
          android:name="androidx.core.content.FileProvider"
          android:authorities="com.example.transfergame.provider"
          android:exported="false"
          android:grantUriPermissions="true"&gt;
          &lt;!-- ressource file to create --&gt;
          &lt;meta-data
              android:name="android.support.FILE_PROVIDER_PATHS"
              android:resource="@xml/file_paths"&gt;
          &lt;/meta-data&gt;
      &lt;/provider&gt;
  &lt;/application&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>File_Paths.xml (in Res folder):</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;paths xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;external-path name="external_files" path="." /&gt;
  &lt;/paths&gt;
  </code></pre>
  
  <p><strong>Main Activity code:</strong></p>
  
  <pre><code>    package com.example.transfergame;
  
  public class ViewPlayerHistoryContents extends AppCompatActivity {
  
      public ImageButton shareButton;
  public static File imagePath;
  
  public Bitmap takeScreenshot() {
      View rootView = findViewById(android.R.id.content).getRootView();
      rootView.setDrawingCacheEnabled(true);
      return rootView.getDrawingCache();
  }
  
  public void saveBitmap(Bitmap bitmap) {
      imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
      FileOutputStream fos;
      try {
          fos = new FileOutputStream(imagePath);
          bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
          fos.flush();
          fos.close();
      } catch (FileNotFoundException e) {
          Log.e("GREC", e.getMessage(), e);
      } catch (IOException e) {
          Log.e("GREC", e.getMessage(), e);
      }
  }
  
  private void shareIt() {
  
      File imageFile = null;
      Uri imageUri = FileProvider.getUriForFile(
              ViewPlayerHistoryContents.this,
              "com.example.transfergame.provider", //(use your app signature + ".provider" )
              imageFile);
  //    Uri uri = Uri.fromFile(imagePath);
      Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
      sharingIntent.setType("image/*");
      String shareBody = "In Tweecher, My highest score with screen shot";
      sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Tweecher score");
      sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
      sharingIntent.putExtra(Intent.EXTRA_STREAM, imageFile);
  
      startActivity(Intent.createChooser(sharingIntent, "Share via"));
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.view_player_history);
  
      shareButton = findViewById(R.id.shareButton);
  
      View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
  
  
      // now setting onClickListener to query button
  
      shareButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Bitmap bitmap = takeScreenshot();
              saveBitmap(bitmap);
              shareIt();
  
          }
      });
  }
  
    }
  </code></pre>
  
  <p>The error message I am getting is this:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.transfergame, PID: 20421
  java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getCanonicalPath()' on a null object reference
      at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:728)
      at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
      at com.example.transfergame.ViewPlayerHistoryContents.shareIt(ViewPlayerHistoryContents.java:149)
      at com.example.transfergame.ViewPlayerHistoryContents.access$000(ViewPlayerHistoryContents.java:30)
      at com.example.transfergame.ViewPlayerHistoryContents$1.onClick(ViewPlayerHistoryContents.java:189)
      at android.view.View.performClick(View.java:6256)
      at android.view.View$PerformClick.run(View.java:24701)
      at android.os.Handler.handleCallback(Handler.java:789)
      at android.os.Handler.dispatchMessage(Handler.java:98)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6541)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  </code></pre>
  
  <p>The line</p>
  
  <pre><code>at com.example.transfergame.ViewPlayerHistoryContents.shareIt(ViewPlayerHistoryContents.java:149)
  </code></pre>
  
  <p>is talking about </p>
  
  <pre><code>Uri imageUri = FileProvider.getUriForFile(
                  ViewPlayerHistoryContents.this,
                  "com.example.transfergame.provider", //(use your app signature + ".provider" )
                  imageFile);
  </code></pre>
  
  <p>in my shareIt() method, but I'm not sure why that is giving me an error as that matches exactly with my authorities in my android manifest file.</p>
  
  <p>Does anyone else know what I am doing wrong?</p>