简体   繁体   English

为什么在Firebase中的onChildAdded中得到NullPointerException?

[英]Why am I getting NullPointerException in onChildAdded in Firebase?

So I am trying to get the data of a 'user' from the Firebase Database. 因此,我尝试从Firebase数据库获取“用户”的数据。 My code was working at first but then I suddenly started getting a NullPointerException. 我的代码起初工作正常,但随后突然开始获得NullPointerException。 I have been trying to get rid of it for about 5 hours now but I don't seem to be getting anywhere. 我已经尝试摆脱它大约5个小时了,但是我似乎什么都没得到。

I have a 'Person' class(To keep the code shorter I have removed the variables and Setters/Getters here: 我有一个'Person'类(为了使代码更短,我在这里删除了变量和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;
 }
}

Then in the 'ProfileFragment' fragment i am doing the following: 然后在“ 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());

     }

   }
 }
}

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. 错误消息很清楚,它说添加的子项之一person.getEmail()Dashboard.email为null时存在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 然后您将获得其中之一为null的信息,然后可以进一步调试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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