简体   繁体   中英

Why am I getting NullPointerException in onChildAdded in Firebase?

So I am trying to get the data of a 'user' from the Firebase Database. My code was working at first but then I suddenly started getting a NullPointerException. I have been trying to get rid of it for about 5 hours now but I don't seem to be getting anywhere.

I have a 'Person' class(To keep the code shorter I have removed the variables and Setters/Getters here:

public class Person {


public Person()
{

}

public Person(String about, String ageRage, String bodytype, String children, String city, String country, String dob, String drink, String education, String email, String ethnicity, String gender, String interests, String latitude, String longitude, String lookingfor, String marital, String password, String profession, String smoke, String username, String imageURL, String religion, String tribe) {
    this.about = about;
    this.ageRage = ageRage;
    this.bodytype = bodytype;
    this.children = children;
    this.city = city;
    this.country = country;
    this.dob = dob;
    this.drink = drink;
    this.education = education;
    this.email = email;
    this.ethnicity = ethnicity;
    this.gender = gender;
    this.interests = interests;
    this.latitude = latitude;
    this.longitude = longitude;
    this.lookingfor = lookingfor;
    this.marital = marital;
    this.password = password;
    this.profession = profession;
    this.smoke = smoke;
    this.username = username;
    this.imageURL = imageURL;
    this.religion = religion;
    this.tribe = tribe;
 }
}

Then in the 'ProfileFragment' fragment i am doing the following:

Override
public void onStart() {
    super.onStart();

usersReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            Person person = dataSnapshot.getValue(Person.class);

            if(person.getEmail().equals(Dashboard.email))
            {
                signedInPerson = person;

                personName.setText(signedInPerson.getUsername());

                offsprings.setText(signedInPerson.getChildren());
                maritalStatus.setText(signedInPerson.getMarital());
                occupation.setText(signedInPerson.getProfession());
                intent.setText(signedInPerson.getLookingfor());
                dob.setText(signedInPerson.getDob());
                bodyType.setText(signedInPerson.getBodyType());
                education.setText(signedInPerson.getEducation());
                ethnicity.setText(signedInPerson.getEthnicity());
                gender.setText(signedInPerson.getGender());
                profession.setText(signedInPerson.getProfession());
                smoke.setText(signedInPerson.getSmoke());
                drink.setText(signedInPerson.getDrink());
                religion.setText(signedInPerson.getReligion());

     }

   }
 }
}

I am getting the exception on this line:

 if(person.getEmail().equals(Dashboard.email))

I have been looking around the internet for a solution haven't found anything that worked. Any help is greatly appreciated.

The error message is clear enough, it says there is a null exception in one of the added child, either person.getEmail() or Dashboard.email is null. From your code i don't know how your Dashboard is defined, Since it works the first time then there might be some network problem and the data is not fetched. You can try debugging the erroneous line in the Terminal Log and find out which one is null ie

Log.e("DEBUG",person.getEmail()) 
Log.e("DEBUG",Dashboard.email) 

if(person.getEmail().equals(Dashboard.email))
{ ......

Then you will get which one of them is null then you can debug further

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM