firebase – Upload image error: Attempt to invoke virtual method 'java.lang.String' on a null object reference-ThrowExceptions

Exception or error:

I have a problem with “capture image with camera”, and store it into Firebase. I think that the code is right, because it worked with “select image from gallery”. The app stoped after capturing the image, and it didn’t store it into the database. I think that it is a problem for android M and N (Android 6 and 7). I just saw other similar questions, but they didn’t work for me. I’m asking for help because I don’t know the solution, thanks. There is also the error in logcat.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){

        Uri uri = data.getData();
        StorageReference filepath = mStorage.child("Photo").child(uri.getLastPathSegment());
        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                Toast.makeText(MainActivity.this, "Upload Done", Toast.LENGTH_LONG).show();




Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String’ on a null object reference

How to solve:

I have an answer for your problem, because I did the same thing just a couple days ago. The problem is that your Uri does not get the image as it should so you need to create your own Uri for your image you capture.

You need to go to android documentation:

For your app, you can also just copy and paste this code in your main activity:

String mCurrentPhotoPath;

private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new     Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */

// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;

private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
    // Create the File where the photo should go
    File photoFile = null;
    try {
        photoFile = createImageFile();
    } catch (IOException ex) {
        // Error occurred while creating the File...
    // Continue only if the File was successfully created
    if (photoFile != null) {
        Uri photoURI = FileProvider.getUriForFile(this,
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
        startActivityForResult(takePictureIntent, CAMERA_REQUEST_CODE);
protected void onCreate(Bundle savedInstanceState) {

storage = FirebaseStorage.getInstance().getReference();

b_gallery = (Button) findViewById(;
b_capture = (Button) findViewById(;
iv_image = (ImageView) findViewById(;

progressDialog = new ProgressDialog(this);

b_capture.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        //Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){
    Uri uri = data.getData();

    StorageReference filepath =     storage.child("Photos").child(uri.getLastPathSegment());
    filepath.putFile(photoURI).addOnSuccessListener(new    OnSuccessListener<UploadTask.TaskSnapshot>() {
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            Toast.makeText(MainActivity.this, "Upload Successful!",    Toast.LENGTH_SHORT).show();
    }).addOnFailureListener(new OnFailureListener() {
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(MainActivity.this, "Upload Failed!", Toast.LENGTH_SHORT).show();

And make sure you follow the documentation and have added the provider in your AndroidManifest.xml:


And the res/xml/file_paths.xml (you just make a directory under “res” folder and name it xml, then you create a resource file and name it file_paths.xml); then delete all the code inside it (it’ll have some lines) and paste the following:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="">
<external-path name="my_images"       path="Android/data/com.serjardovic.firebasesandbox/files/Pictures" />

make sure you CHANGE the com.serjardovic.firebasesandbox to your own package name!

It works 100% – have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *