Categories
Database Development Firebase

Android app crashes when deleting user from firebase Realtimedatabase

I am working on my App, and till now the App runs fine despite some small errors. The user can sign in, login and change his profile information. In the last steps, I added a function to delete userinformation. The delete function works so the userinformation gets deleted from FirebaseAuth and the FirebaseRealtimeDatabase but the app crashes.
Before I already had problems with passing null values, maybe thats related to the problem.

Update: I changed the code and tried to implement the suggestions but its still crashing…

Logcat:

09-18 16:28:20.474 23342-23342/com.example.login E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.login, PID: 23342
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.login.UserProfil.getVorname()' on a null object reference
    at com.example.login.ProfileActivity$1.onDataChange(ProfileActivity.java:59)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.0.0:75)
    at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.0.0:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.0.0:55)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:234)
    at android.app.ActivityThread.main(ActivityThread.java:5526)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Some part of my ProfileActivity

 firebaseAuth=FirebaseAuth.getInstance();
    firebaseDatabase= FirebaseDatabase.getInstance();



    if (firebaseAuth.getCurrentUser() != null) {

        firebaseDatabase= FirebaseDatabase.getInstance();

        final DatabaseReference databaseReference = firebaseDatabase.getReference("Users").child(firebaseAuth.getUid());

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
               if (dataSnapshot.exists()) ;
                {
                    UserProfil userProfil = dataSnapshot.getValue(UserProfil.class);
                    profilVorname.setText(userProfil.getVorname());
                    profilNachname.setText(userProfil.getNachname());
                    profilStrasse.setText(userProfil.getStrasse());
                    profilHNr.setText(userProfil.getHnr());
                    profilPlz.setText(userProfil.getPlz());
                    profilStadt.setText(userProfil.getStadt());
                    profilLand.setText(userProfil.getLand());
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(ProfileActivity.this, "Database Error", Toast.LENGTH_SHORT).show();
            }
        });
    }else{
        startActivity(new Intent(ProfileActivity.this,NavActivity.class));
    }

Some part of my UpdatProfilActivity

loeschen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final DatabaseReference databaseReference = firebaseDatabase.getReference("Users").child(firebaseAuth.getUid());
            databaseReference.removeValue();
            FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();

            user.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if(task.isSuccessful()){
                        // firebaseAuth.signOut();
                        // startActivity(new Intent(UpdateProfilActivity.this, MainActivity.class));
                        //finish();
                    }

                }
            });
        }
    });

Leave a Reply

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