为什么我在Firebase中的onChildAdded中遇到NullPointerException?

时间:2018-05-05 21:29:16

标签: android firebase firebase-realtime-database

所以我试图获取“用户”的数据。来自Firebase数据库。我的代码首先工作但后来我突然开始得到一个NullPointerException。我一直试图摆脱它约5个小时,但我似乎没有到达任何地方。

我有一个' Person' class(为了缩短代码,我已经删除了变量和Setters / Getters:

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

然后在' ProfileFragment'片段我正在做以下事情:

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());

     }

   }
 }
}

我在这一行得到例外:

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

我一直在互联网上寻找解决方案,找不到任何有效的方法。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

错误消息足够清楚,它表示在其中一个添加的子项中存在空异常,person.getEmail()Dashboard.email为空。从您的代码我不知道您的仪表板是如何定义的,因为它第一次工作然后可能存在一些网络问题并且不提取数据。您可以尝试调试终端日志中的错误行,找出哪一行是空的,即

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

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

然后你会得到哪一个是null然后你可以进一步调试