Here is my database structure. First i want to search a person in fire-base database and then add then in logged-in account as a child under "FriendList" node.
The query which i implemented gives me correct result and it also add the value as child in the logedin account but when i want to add the second person in "Friendlist" node or want to search the data the app get crashes and give NullPointerException error. I have added my search code.
public void SearchData(final String search, final int flag){
if(flag==1){
arfullname.clear();
aridkey.clear();
arpassword.clear();
arfirstname.clear();
aremail.clear();
arlastname.clear();
}
Query query = mDatabase.orderByChild("fullname");
query.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
for (DataSnapshot childd:dataSnapshot.getChildren()){
Log.d("TAG123",""+childd.child("fullname").getValue().toString().equals(search));
if(childd.child("fullname").getValue().toString().contains(search) ){
firstname=childd.child("firstname").getValue().toString();
lastname=childd.child("lastname").getValue().toString();
email=childd.child("email").getValue().toString();
fullname=childd.child("fullname").getValue().toString();
password=childd.child("password").getValue().toString();
idkey=childd.child("key").getValue().toString();
arfirstname.add(firstname);
arlastname.add(lastname);
aremail.add(email);
arfullname.add(fullname);
arpassword.add(password);
aridkey.add(idkey);
Log.d("customdata",firstname+"}"+lastname+"}"+fullname+"}"+idkey);
personArrayListModel=new PersonArrayListModel(arfullname,arfirstname,arlastname,aridkey,aremail,arpassword);
check=true;
}
else
{
Toast.makeText(MainActivity.this, "Not Found", Toast.LENGTH_SHORT).show();
}
}
if(check==true && flag==1){
ArrayList<CustomFirebaseClass> customFirebaseClasses=getData();
CustomFirebaseAdapter customFirebaseAdapter=new CustomFirebaseAdapter(getApplicationContext(),0,customFirebaseClasses);
listView.setAdapter(customFirebaseAdapter);
Log.d("person123",personArrayListModel.getFullname().toString());
}
if(flag==2){
Toast.makeText(MainActivity.this, "222", Toast.LENGTH_SHORT).show();
Log.d("persondata",firstname+""+lastname+""+fullname+""+email+""+password+""+idkey);
User userr = new User(firstname,lastname,fullname,email,password,idkey);
try{
databaseReference.child(mAuth.getUid()).child("FirendsList").child(idkey).setValue(userr);
}catch (RuntimeException e){
Toast.makeText(getApplicationContext(), ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Comments
Post a Comment