title: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference
body: <p>I'm trying to upload image to firebase storage after capturing it from phone camera but when I run my code, I get the above error. I have tried to check if the URI returned is null as shown in this line of code </p>
  
  <p><code>Toast.makeText(UploadImage.this, "YOUR URI IS NULL", Toast.LENGTH_LONG).show();</code> </p>
  
  <p>but the Toast message does not get displayed, but error is thrown in the line following the toast which is </p>
  
  <p><code>filepath = storageReference.child("PhotoModel").child(uri.getLastPathSegment());</code></p>
  
  <p>Below is my whole code,</p>
  
  <pre><code>public class UploadImage extends AppCompatActivity {
  
  private Button mUploadImg;
  private ImageView mImageView;
  private StorageReference storageReference, filepath;
  private ProgressDialog progressDialog;
  private static final int CAMERA_REQUEST_CODE = 1;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_upload_image);
      storageReference = FirebaseStorage.getInstance().getReference();
  
      mUploadImg = (Button) findViewById(R.id.btnUploadImg);
      mImageView = (ImageView) findViewById(R.id.imgView);
      progressDialog = new ProgressDialog(this);
      mUploadImg.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              Intent Imgintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              startActivityForResult(Imgintent, CAMERA_REQUEST_CODE);
  
          }
      });
  
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
      if (requestCode == CAMERA_REQUEST_CODE &amp;&amp; resultCode == RESULT_OK) {
          progressDialog.setMessage("Upload Image ...");
          progressDialog.show();
  
          Uri uri = data.getData();
          if (uri == null) {
  
  
              Toast.makeText(UploadImage.this, "YOUR URI IS NULL", Toast.LENGTH_LONG).show();
          }
          filepath = storageReference.child("PhotoModel").child(uri.getLastPathSegment());
          filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener&lt;UploadTask.TaskSnapshot&gt;() {
              @Override
              public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                  progressDialog.dismiss();
  
                  Toast.makeText(UploadImage.this, "Uploading ...", Toast.LENGTH_LONG).show();
              }
          });
  
      } else {
          Toast.makeText(UploadImage.this, "Upload Failed...Check your Internet Connection", Toast.LENGTH_LONG).show();
      }
    }
   }
  </code></pre>
  
  <p>Someone help me figure out where the problem starts, because I can't really put a finger on it right now...</p>