javaandroidfirebaseitemtouchhelper

How Can I delete an entry from realtime database using onSwipe on a recyclerview


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


Solution

  • 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
    }