2

Everyone I am a new learner of Android Studio, and on our school project, we have three tabs 'album', 'notes', and 'files' and I am getting errors when deleting a document from their subcollection.

For example, this is how we store images on firestore:

private void uploadToFirebase(Uri uri) {
    Log.d("UploadToFirebase", "Starting upload process");
    String caption = uploadCaption.getText().toString();
    final StorageReference imageReference = storageReference.child("Album" + System.currentTimeMillis() + "." + getFileExtension(uri));
    final CollectionReference forAlbum = Utility.getCollectionReferenceForAlbum(courseId);

    imageReference.putFile(uri)
      .addOnSuccessListener(taskSnapshot -> {
        // Get download URL for the uploaded image
        imageReference.getDownloadUrl().addOnSuccessListener(uriResult -> {
          String imageUrl = uriResult.toString();

          DataClass dataClass = new DataClass(imageUrl, caption);

          forAlbum.add(dataClass)
          .addOnSuccessListener(CollectionReference -> {
            progressBar.setVisibility(View.INVISIBLE);
            Toast.makeText(AddtoAlbum.this, "Uploaded", Toast.LENGTH_SHORT).show();
            finish();
          })
          .addOnFailureListener(e -> {
            progressBar.setVisibility(View.INVISIBLE);
            Toast.makeText(AddtoAlbum.this, "Failed to upload image", Toast.LENGTH_SHORT).show();
          });
        });
      })

As you can see above I have two way storing, on firebase storage and firebase firestore. And I have this new Activity to view the image and inside that is a delete option, however every time I hit delete icon on our app it just loads to another page and crashes when I repeat the process. What could possibly the reason of this error?

Here is my way of deleting the image in firebase:

private void deleteImage(String imageUrl) {
  CollectionReference imagesCollectionRef = FirebaseFirestore.getInstance().collection("Albums");
  DocumentReference imageDocRef = imagesCollectionRef.document(imageUrl);

  imageDocRef.delete()
    .addOnSuccessListener(new OnSuccessListener < Void > () {
      @Override
      public void onSuccess(Void aVoid) {
        Toast.makeText(FullImage.this, "Image deleted", Toast.LENGTH_SHORT).show();
        finish();
      }
    })
    .addOnFailureListener(new OnFailureListener() {
      @Override
      public void onFailure(@NonNull Exception e) {
        Toast.makeText(FullImage.this, "Failed to delete image", Toast.LENGTH_SHORT).show();
      }
    });
}

I tried to put logs on it but it doesn't show anything it just loads to previous page. Ive been trying to delete the files on storage instead but I am getting so much error.

This what I got on logcat:

FATAL EXCEPTION: main
Process: com.example.project_acadeamease1, PID: 10281
java.lang.NullPointerException: Provided document path must not be null.
at com.google.firebase.firestore.util.Preconditions.checkNotNull(Preconditions.java: 148)
at com.google.firebase.firestore.CollectionReference.document(CollectionReference.java: 103)
at com.example.project_acadeamease1.Utility.getCollectionReferenceForAlbum(Utility.java: 25)
at com.example.project_acadeamease1.FullImage.deleteImage(FullImage.java: 59)
at com.example.project_acadeamease1.FullImage.access$100(FullImage.java: 20)
at com.example.project_acadeamease1.FullImage$2.onClick(FullImage.java: 51)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java: 188)
at android.os.Handler.dispatchMessage(Handler.java: 106)
at android.os.Looper.loop(Looper.java: 246)
at android.app.ActivityThread.main(ActivityThread.java: 8653)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java: 602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 1130)
2
  • 2
    When an app crashes, it writes an error message and stack trace to its logcat. Please find those, and add them to your question by clicking the edit link under it. Also see stackoverflow.com/questions/23353173/… and stackoverflow.com/questions/3988788/… Commented Feb 16, 2024 at 2:16
  • The error message "Provided document path must not be null" should make you consider what is the value of imageUrl at the time you use it to build a document reference. Commented Feb 16, 2024 at 13:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.