简体   繁体   中英

[SOLVED]Firebase returns null value for a particular child even if it exists

[EDIT]The problem was with my ComRec class. The getKoil method didn't work for some reason.I generated it using studio and now its working fine .Thanks for the help guys.[EDIT] I have a class ComRec which stores certain commission values. In the calculator I input some values and perform some operations then store it under particular user in firebase. Then I create a pdf table with the values from ComRec object. But my problem is the member "koil" shows the value null even if it is present in the ComRec object.

public class ComRec {
private Double freeRation;
private Double npnsRice;
private Double npsRice;
private Double phhRice;
private Double aayRice;
private Double npnsWheat;
private Double phhWheat;
private Double aayWheat;
private Double atta;
private Double total;
private Double koil;
private Double sugar;
private Double totalCommission;

public ComRec(){

}

public ComRec(Double freeRation, Double npnsRice, Double npsRice, Double phhRice, Double aayRice, Double npnsWheat, Double phhWheat, Double aayWheat, Double atta, Double total, Double koil, Double sugar , Double totalCommission) {
    this.freeRation = freeRation;
    this.npnsRice = npnsRice;
    this.npsRice = npsRice;
    this.phhRice = phhRice;
    this.aayRice = aayRice;
    this.npnsWheat = npnsWheat;
    this.phhWheat = phhWheat;
    this.aayWheat = aayWheat;
    this.atta = atta;
    this.total = total;
    this.koil = koil;
    this.sugar = sugar;
    this.totalCommission = totalCommission;

}

User class

public class User {
private String UID;
private String ardNo;
private String email;
private String serialKey;
private String taluk;
private ComRec C;

public User(){

}

public User(String UID, String ardNo, String email, String serialKey,String taluk) {
    this.UID = UID;
    this.ardNo = ardNo;
    this.email = email;
    this.serialKey = serialKey;
    this.taluk = taluk;
    this.C = null;
}
public User(String UID, String ardNo, String email, String serialKey,String taluk,ComRec C) {
    this.UID = UID;
    this.ardNo = ardNo;
    this.email = email;
    this.serialKey = serialKey;
    this.taluk = taluk;
    this.C = C;
}

public String getUID() {
    return UID;
}

public void setUID(String UID) {
    this.UID = UID;
}

public String getArdNo() {
    return ardNo;
}

public void setArdNo(String ardNo) {
    this.ardNo = ardNo;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getSerialKey() {
    return serialKey;
}

public void setSerialKey(String serialKey) {
    this.serialKey = serialKey;
}

public String getTaluk() {
    return taluk;
}

public void setTaluk(String taluk) {
    this.taluk = taluk;
}

public ComRec getComRec() {
    return C;
}

public void setComRec(ComRec c) {
    C = c;
}

}

This is my firebase data, You can see koil = 172.5:

2

All the other members gives correct value but koil

1个

This is how initialise ComRec object by taking it from User class

3

Base on your database structure you can use this to get the Koil

  DatabaseReference reff = FirebaseDatabase.getInstance().getReference("User");
    reff.child(user.getUid()).child("comRec").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot snapshot:dataSnapshot.getChildren()){
                ComRec com = snapshot.getValue(ComRec.class);
                Double Koil = com.getkoil();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

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