簡體   English   中英

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

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

因此,我嘗試從Firebase數據庫獲取“用戶”的數據。 我的代碼起初工作正常,但隨后突然開始獲得NullPointerException。 我已經嘗試擺脫它大約5個小時了,但是我似乎什么都沒得到。

我有一個'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;
 }
}

然后在“ 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為null時存在null異常。 從您的代碼中,我不知道您的儀表板是如何定義的,因為它是第一次工作,所以可能存在一些網絡問題,並且無法獲取數據。 您可以嘗試調試終端日志中的錯誤行,並找出哪一條為空,即

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

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

然后您將獲得其中之一為null的信息,然后可以進一步調試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM