0

I am making an app to click photos and then upload the same on Firebase storage but I am getting this nullpointer exception in my code.

I have tried bitmap image too but it didn't help as I am still getting the same error. This is my code below of the onActivityResult method:

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==CAMERA_REQUEST_CODE&& resultCode==RESULT_OK){

            Uri uri=data.getData();
            StorageReference filepath =mStorage.child("Photos").child(uri.getLastPathSegment());

           filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                 Uri downloadUri = taskSnapshot.getUploadSessionUri();


                    Toast.makeText(MainActivity.this,"upload finished",Toast.LENGTH_LONG).show();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(MainActivity.this, "Sending failed", Toast.LENGTH_SHORT).show();

                }
            });
        }
    }
}

The error that I am getting:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference

Dino
  • 7,779
  • 12
  • 46
  • 85
  • 1
    `@Nullable Intent data`. Looks like `data` is null. – Johannes Kuhn Sep 23 '19 at 03:08
  • 4
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Max Vollmer Sep 23 '19 at 03:11
  • 1
    Try either using a debugger (best) or add some `println()` output to see what's going on with `data`. Specifically, it looks like `data.getData()` is returning null which is not what you're expecting. I would dig into that to figure out what's going on. – Kaan Sep 23 '19 at 03:18

0 Answers0