0

I am looking to delete an entry using onSwipe, I have this code below that works to swipe but it does not delete from firebase, when I swipe the app crashes. see my code below and the logcat error.

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        expenses.remove(viewHolder.getAdapterPosition());
        adapter.notifyDataSetChanged();

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Expenses").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
        reference.child(postId).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Toast.makeText(ExpensesActivity.this, "Expenditure deleted!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(ExpensesActivity.this, "Something went wrong, try again" + task.getException(), Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

This is the logcat message;

2022-03-21 14:44:23.365 22734-22734/com.delossantos.expensapp D/AndroidRuntime: Shutting down VM
2022-03-21 14:44:23.366 22734-22734/com.delossantos.expensapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.delossantos.expensapp, PID: 22734
    java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
        at com.google.firebase.database.DatabaseReference.child(DatabaseReference.java:96)
        at com.delossantos.expensapp.ExpensesActivity$6.onSwiped(ExpensesActivity.java:270)
        at androidx.recyclerview.widget.ItemTouchHelper$4.run(ItemTouchHelper.java:712)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

enter image description here

  • It is saying that something that is required is null. Check if your method is receiving values for `postId` at line 6. If yes, you might have some other issues with how you are getting the collection reference from firebase. – Estevao Santiago Mar 21 '22 at 19:57
  • 1
    It looks like your `postId` variable is `null`. If that is supposed to be possible, you'll want to check for that in your code. – Frank van Puffelen Mar 21 '22 at 20:23
  • If you consider at some point in time to try using [Cloud Firestore](https://firebase.google.com/docs/firestore/), I think that this article, [How to delete a record from Firestore on a RecylerView left/right swipe?](https://medium.com/firebase-tips-tricks/how-to-delete-a-record-from-firestore-on-a-recylerview-left-right-swipe-d65d993f0baf) or [How to delete multiple records from Firestore using RecyclerView multi-selection?](https://medium.com/firebase-tips-tricks/how-to-delete-multiple-records-from-firestore-using-recyclerview-multi-selection-96108e4c6166) might help. – Alex Mamo Mar 22 '22 at 06:42

1 Answers1

0

Looks like your getUid() is null. You can use below codes to check if the user is currenty logged in or not.

final FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
if (fUser != null && postId != null) {
    final String userUid = fUser.getUid();
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Expenses").child(userUid);
    reference.child(postId).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Toast.makeText(ExpensesActivity.this, "Expenditure deleted!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(ExpensesActivity.this, "Something went wrong, try again" + task.getException(), Toast.LENGTH_SHORT).show();
                }
            }
        });
} else {
    //User is missing, do something
}
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46
  • I do not have issues with the current user, the problem is with the id for the specific item I want to remove, every item is saved with an id, I need my swipe to know that's the id I want to remove when I swipe on the item in the recyclerview. I just added a photo of the firebase table, please check. – Yeury De los Santos Mar 21 '22 at 21:54
  • @YeuryDelosSantos I think your `postId` is `null`. As @Frank said, you need to check where do you get your `postId`, can you share the code for `postId`? – Ticherhaz FreePalestine Mar 22 '22 at 00:13