Android 为什么我在Firebase的OnChildaded中得到NullPointerException?

Android 为什么我在Firebase的OnChildaded中得到NullPointerException?,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,所以我试图从Firebase数据库中获取“用户”的数据。我的代码一开始是有效的,但后来我突然发现一个NullPointerException。我已经试着摆脱它大约5个小时了,但我似乎没有取得任何进展 我有一个“Person”类来缩短代码,我在这里删除了变量和setter/getter: public class Person { public Person() { } public Person(String about, String ageRage, String bodytype,

所以我试图从Firebase数据库中获取“用户”的数据。我的代码一开始是有效的,但后来我突然发现一个NullPointerException。我已经试着摆脱它大约5个小时了,但我似乎没有取得任何进展

我有一个“Person”类来缩短代码,我在这里删除了变量和setter/getter:

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

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

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

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

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

然后,您将获得其中哪一个为空,然后您可以进一步调试

这意味着在这个构造中person.getEmail.equalsDashboard.email getEmail或Dashboard为/返回空。根据所提供的信息,我们不可能说得更多。您可能需要记录dataSnapshot.childemail.getValue,以查看这是否显示您期望的值。person.getEmail肯定返回null。Dashboard.email可能为空,这不会导致问题,因为您可以在不发生崩溃的情况下将null传递给equals,所以它知道如何检查null。Java虚拟机规范不要求具体的值编码null。因此,如果Dashboard.email为null,则会发生错误。您在实践中见过这种情况吗?不,请不要将null传递给equals部分。True它不会在equals方法中导致崩溃,但在使用时会抛出错误,而不检查它在其他实例中是否为null