所以我试图将一个用户的个人资料加载到一个对象中,事情就是程序确实获得了我所拥有的参考文件的所需密钥,但是当应用程序获取了Profile对象时所发生的错误getters返回null。
这是我的日志打印:
W/ClassMapper: No setter/field for -K2Hh5c8ltlwIXNO44Rp found on class
com.example.android.firetest.models.Profiles
I/System.out: the user's id: null
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.firetest, PID: 8546
java.lang.NullPointerException: Attempt to invoke virtual method'
java.lang.String java.lang.String.trim()' on a null object reference
at java.lang.StringToReal.parseDouble(StringToReal.java:263)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.android.firetest.Account_balance$2.onDataChange(Account_balance.java:263)
我的方法来检索用户数据:
private void loadUserdata() {
System.out.println("entro aqui !");
Query profileQuery = myRef.child("profiles").orderByChild("email").equalTo("jose.nieto@riagency.us").limitToFirst(1);
profileQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Profiles user = dataSnapshot.getValue(Profiles.class);
System.out.println("the user's id: "+user.getId());
base_salary = user.getBase_salary();
cedula = user.getId();
//Here is where the error happens
Double monto_quincenal = Double.parseDouble(base_salary)/2;
primera_quincena.setText(monto_quincenal.toString());
segunda_quincena.setText(monto_quincenal.toString());
Account_name.setText(user.getFirst_name() +" "+ user.getLast_name());
refOrder = myRef.child("invoices").child(year.getSelectedItem().toString())
.child(month.getSelectedItem().toString())
.orderByChild("id").equalTo(cedula);
System.out.println("la cedula en el metodo "+cedula);
populateInvoiceList();
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("the has been an error retrieving the user data: \n" + databaseError.getMessage());
}
});
}
我真的不知道为什么在下面已经分配了base_salary为空时
POJO:
public class Profiles {
private String age;
private String email;
private String name;
private String image;
private String first_name;
private String address;
private String avatar;
private String base_salary;
private String birthday_date;
private String city;
private String counrty_code;
private String dpto;
private String id;
private Boolean is_leader;
private String last_name;
private String occupation;
private String phone;
private int registered_at;
private String role;
private Boolean status;
private int updated_at;
public Profiles() {
}
public Profiles(String age, String email, String name, String first_name,
String image, String address, String avatar, String base_salary, String counrty_code, String birthday_date,
String city, String dpto, String id, Boolean is_leader, String occupation, String last_name,
String phone, int registered_at, String role, Boolean status, int updated_at) {
//**
}
Firebase参考:
firetest-e5a93/profiles/-K2Hh5c8ltlwIXNO44Rp
-K2Hh5c8ltlwIXNO44Rp
address: "cra 45, calle 75b sur, int 106, edif las acasia..."
avatar: "avatar.jpg"
base_salary: "300000"
birthday_date: "1986-01-03"
city: "sabaneta"
counrty_code: "CO"
dpto: 2
email: "jose.nieto@riagency.us"
first_name: "Jose Gabriel"
has_avatar: true
id: "060514699"
interests: "PHP, Js, html, css, AngularJS, laravel, cakephp..."
is_leader: true
last_name: "Nieto Avila"
occupation: "Ing en Informatica"
phone: "3223349524"
position: "Lider Development"
registered_at: 1446643984181
role: "admin"
schedule_special: true
status: true
team_name: "Fino Code"
updated_at: 1446643984181
正如你所看到的,在Log中我们得到了相同的 Key 但是Profile对象似乎是null,有什么想法吗?
答案 0 :(得分:0)
您的POJO中的年龄属性不在您的firebase引用中,而您的firebase引用中的team_name不在您的POJO中。
哦,而且,该查询将返回一个Profiles数组,而不是一个Profile。这就是为什么它试图将该密钥设置为属性的原因。尝试获取快照的第一个孩子。
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Profiles profile = snapshot.getValue(Profiles.class);
System.out.println(profile);
}