Deleting user data from Firebase realtime database

DerHammer123

I'm still working on my Login App. At the moment the user can register, sign in and update profile information. The data is stored in firebase realtime database and the athentificacion also works via firebase. I wanted to add a "Delete Profile" button, so the user information is deleted from firebase. I figuered out how to delete the user from the autentification process. Now I tried to call the remove() function to the Uid, so now the data gets deleted in the real time database but is still in the Firebase authentification and the app crashes if you delete the userdata. So only one of the deleting processes works... either the autentification or deleting the user from the database. Maybe the problem is about structure and child/parent relation in the realtimedatabase? My suggestion is that the order in onClickListener() could be incorrect? How can i delete both of it at once and get this process working? Thx in advance :)

public class UpdateProfilActivity extends AppCompatActivity {

private EditText newUserVorname, newUserNachname,newUserStrasse,newUserHnr,newUserPlz,newUserStadt,newUserLand;
private Button speichern;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_update_profil);

    newUserVorname=findViewById(R.id.editTextVornameUpdate);
    newUserNachname=findViewById(R.id.editTextNachnameUpdate);
    newUserStrasse=findViewById(R.id.editTextStrasse);
    newUserHnr=findViewById(R.id.editTextHNr);
    newUserPlz=findViewById(R.id.editTextPlz);
    newUserStadt=findViewById(R.id.editTextStadt);
    newUserLand=findViewById(R.id.editTextLand);
    speichern=findViewById(R.id.buttonSpeichern);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

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

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

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            UserProfil userProfil=dataSnapshot.getValue(UserProfil.class);
            newUserVorname.setText(userProfil.getVorname());
            newUserNachname.setText(userProfil.getNachname());
            newUserStrasse.setText(userProfil.getStrasse());
            newUserHnr.setText(userProfil.getHnr());
            newUserPlz.setText(userProfil.getPlz());
            newUserStadt.setText(userProfil.getStadt());
            newUserLand.setText(userProfil.getLand());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(UpdateProfilActivity.this,"Database Error",Toast.LENGTH_SHORT).show();
        }
    });

    speichern.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            String Vorname = newUserVorname.getText().toString();
            String Nachname = newUserNachname.getText().toString();
            String Strasse = newUserStrasse.getText().toString();
            String HNr = newUserHnr.getText().toString();
            String Plz = newUserPlz.getText().toString();
            String Stadt = newUserStadt.getText().toString();
            String Land = newUserLand.getText().toString();


            UserProfil userProfil=new UserProfil(Vorname,Nachname,Strasse,HNr,Plz,Stadt,Land);

            databaseReference.setValue(userProfil);

            finish();

        }

    });

}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch(item.getItemId()){
        case android.R.id.home:
            onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}

UserProfil

public class UserProfil {
public String vorname;
public String nachname;
public String strasse;
public String hnr;
public String plz;
public String stadt;
public String land;

public UserProfil(){

}


public UserProfil(String Vorname, String Nachname,String Strasse,String HNr,String Plz,String Stadt,String Land) {
    this.vorname = Vorname;
    this.nachname = Nachname;
    this.strasse= Strasse;
    this.hnr= HNr;
    this.plz=Plz;
    this.stadt=Stadt;
    this.land=Land;

}

public String getVorname() {
   return vorname;
}

public void setVorname(String vorname) {
    this.vorname = vorname;
}

public String getNachname() {
    return nachname;
}

public void setNachname(String nachname) {
    this.nachname = nachname;
}

public void setStrasse(String strasse) {
    this.strasse = strasse;
}

public String getStrasse() {
    return strasse;
}

public void setHnr(String hnr) {
    this.hnr = hnr;
}

public String getHnr() {
    return hnr;
}

public void setPlz(String plz) {
    this.plz = plz;
}

public String getPlz() {
    return plz;
}

public void setStadt(String stadt) {
    this.stadt = stadt;
}

public String getStadt() {
    return stadt;
}

public void setLand(String land) {
    this.land = land;
}

public String getLand() {
    return land;
}

Here is the logcat of the crash

Process: com.example.login, PID: 29022
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.login/com.example.login.ProfileActivity}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in FirebaseDatabase.getReference()
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
    at android.app.ActivityThread.access$900(ActivityThread.java:154)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    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)
 Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in FirebaseDatabase.getReference()
    at com.google.firebase.database.FirebaseDatabase.getReference(com.google.firebase:firebase-database@@19.0.0:164)
    at com.example.login.ProfileActivity.onCreate(ProfileActivity.java:49)
    at android.app.Activity.performCreate(Activity.java:6285)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
    at android.app.ActivityThread.access$900(ActivityThread.java:154) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    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) 

EDIT:

Prolem occurs in following line: DatabaseReference databaseReference= firebaseDatabase.getReference(firebaseAuth.getUid());

public class ProfileActivity extends AppCompatActivity {

private TextView profilVorname,profilNachname,profilStrasse,profilHNr,profilPlz,profilStadt,profilLand;
private Button profilUpdate,PasswortUpdate;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    profilVorname= findViewById(R.id.textViewPVorname);
    profilNachname=findViewById(R.id.textViewPNachname);
    profilUpdate=findViewById(R.id.buttonProfilUpdate);
    profilStrasse=findViewById(R.id.textViewPStrasse);
    profilHNr=findViewById(R.id.textViewPHNr);
    profilPlz=findViewById(R.id.textViewPPlz);
    profilStadt=findViewById(R.id.textViewPStadt);
    profilLand=findViewById(R.id.textViewPLand);
    PasswortUpdate=findViewById(R.id.buttonPasswordUpdate);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

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

    DatabaseReference databaseReference= firebaseDatabase.getReference(firebaseAuth.getUid());

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            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();
        }
    });



    profilUpdate.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            startActivity(new Intent(ProfileActivity.this,UpdateProfilActivity.class));

        }
    });

    PasswortUpdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(ProfileActivity.this,UpdatePasswortActivity.class));
        }
    });
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch(item.getItemId()){
        case android.R.id.home:
            onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent(ProfileActivity.this, NavActivity.class));
}
Alex Mamo

You are getting the following error:

Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in FirebaseDatabase.getReference()

Because you are passing to the getReference() method in the following line of code an object that has the value of null:

DatabaseReference databaseReference= firebaseDatabase.getReference(firebaseAuth.getUid());

So firebaseAuth.getUid() return null because firebaseAuth object is null meaning that your user is not authenticated.

To solve this, first of all make sure your user is authenticated. Second, to actually get the uid you need to call getUid() on the FirebaseUser object and not on the FirebaseAuth object. So please change the above line of code to:

String uid = firebaseAuth.getCurrentUser().getUid();
//                           ^     ^
DatabaseReference databaseReference= firebaseDatabase.getReference(uid);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Retrieve a single data from Firebase Realtime Database

How to retrieve data from realtime database in Firebase?

How to store data in realtime database in firebase during user registration?

Retrieving data from Firebase Realtime Database in Android

How to save new user data to Firebase Realtime Database after Login

Retrieve history data from firebase realtime database

Getting data from FIrebase realtime database

Read data from firebase realtime database

Read specific data from a Firebase Realtime Database

How to read data from the Firebase realtime database

Firebase: retrieve data from realtime database as an Int

Retrieve child data from firebase realtime database

How to select data from firebase realtime database

Displaying Data from Firebase Realtime Database

Streaming data from Firebase Realtime Database

How to retrieve user email from Firebase Authentication to Realtime Database

Unable to fetch data From Firebase Realtime Database

Couldnot retrieve data from firebase realtime database

Display data from Firebase Realtime Database

How to enable user to delete only specific data from Firebase Realtime Database

Firebase realtime database filtering data

Read from Firebase Realtime Database where reference = User UID

Firebase Realtime Database Only Retrieving data with User's ID

Deleting User data in firebase Database not working

Read data from Realtime Database from Firebase in SwiftUI

Not able to read data from Firebase realtime database

Android Studio Firebase realtime database always denies deleting data

Firebase realtime database, put data only if the user not exist

How to store data for each user in Flutter Firebase Realtime Database?