Firebase Firestore:访问数据时返回NullPointer异常

时间:2018-03-15 18:38:08

标签: java android firebase google-cloud-firestore

我想做什么?

从某些集合访问Firebase Firestore数据,将其附加到类中以进行简单的数学运算。

我的问题是什么?

我收到此错误:

FATAL EXCEPTION: main
Process: io.nourish.strongerasfukk, PID: 11299
      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[])' on a null object reference
      at com.google.android.gms.internal.zzevb$zza.zza(Unknown Source)
      at com.google.android.gms.internal.zzevb.zza(Unknown Source)
      at com.google.android.gms.internal.zzevb.zza(Unknown Source)
      at com.google.firebase.firestore.DocumentSnapshot.toObject(Unknown Source)
      at io.nourish.strongerasfukk.waves.ten.Tw_WeekOne$1.onComplete(Tw_WeekOne.java:56)
      at com.google.android.gms.tasks.zzf.run(Unknown Source)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5466)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

代码吗

public class Tw_WeekOne extends Fragment {

private FirebaseFirestore db;
private FirebaseUser user;
private String uID;
private DocumentReference weights;
private WeightsLifted wl;

public Tw_WeekOne() {}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    user = FirebaseAuth.getInstance().getCurrentUser();
    uID = user.getUid();
    db = FirebaseFirestore.getInstance();
    Toast.makeText(getActivity(), uID, Toast.LENGTH_SHORT).show();

    weights = db.collection("weight-lifted").document(uID);
    weights.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if(task.isSuccessful()) {
                DocumentSnapshot documentSnapshot = task.getResult();
                if(documentSnapshot != null && documentSnapshot.exists()) {
                    wl = documentSnapshot.toObject(WeightsLifted.class);
                }
            }

        }
    });

}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tw_weekone, container, false);
    final TextView txt = v.findViewById(R.id.ajsdjh);
    return v;
}

public class WeightsLifted {
    private String pressBancaKg;
    private String pressBancaRep;
    private String sentadillaKg;
    private String sentadillaRep;
    private String pesoMuertoKg;
    private String pesoMuertoRep;
    private String pressMilitarKg;
    private String pressMilitarRep;

    public WeightsLifted(String pressBancaKg, String pressBancaRep, String sentadillaKg, String sentadillaRep, String pesoMuertoKg, String pesoMuertoRep, String pressMilitarKg, String pressMilitarRep) {
        this.pressBancaKg  = pressBancaKg;
        this.pressBancaRep = pressBancaRep;
        this.sentadillaKg  = sentadillaKg;
        this.sentadillaRep = sentadillaRep;
        this.pesoMuertoKg  = pesoMuertoRep;
        this.pesoMuertoRep = pesoMuertoRep;
        this.pressMilitarKg  = pressMilitarKg;
        this.pressMilitarRep = pressMilitarRep;
    }

    public String getPressBancaKg()  { return pressBancaKg; }
    public String getPressBancaRep() { return pressBancaRep; }
    public String getSentadillaKg()  { return sentadillaKg; }
    public String getSentadillaRep() { return sentadillaRep; }
    public String getPesoMuertoKg()  { return pesoMuertoKg; }
    public String getPesoMuertoRep() { return pesoMuertoRep; }
    public String getPressMilitarKg() { return pressMilitarKg; }
    public String getPressMilitarRep() { return pressMilitarRep; }

    public void setPressBancaKg(String pressBancaKg)       { this.pressBancaKg = pressBancaKg; }
    public void setPressBancaRep(String pressBancaRep)     { this.pressBancaRep = pressBancaRep; }
    public void setSentadillaKg(String sentadillaKg)       { this.sentadillaKg = sentadillaKg; }
    public void setSentadillaRep(String sentadillaRep)     { this.sentadillaRep = sentadillaRep; }
    public void setPesoMuertoKg(String pesoMuertoKg)       { this.pesoMuertoKg = pesoMuertoKg; }
    public void setPesoMuertoRep(String pesoMuertoRep)     { this.pesoMuertoRep = pesoMuertoRep; }
    public void setPressMilitarKg(String pressMilitarKg)   { this.pressMilitarKg = pressMilitarKg; }
    public void setPressMilitarRep(String pressMilitarRep) { this.pressMilitarRep = pressMilitarRep; }
}

正如你所看到的,我得到的错误就在这一行:

  

wl = documentSnapshot.toObject(WeightsLifted.class);

我阅读了文档,我看到了一些以这种方式实现的示例,但是他们没有解释如何创建类来使用它来附加Firestore中的数据。

我在网上寻找并找不到我的问题的解决方案。所以,我被困了。非常感谢任何帮助。

提前致谢。

1 个答案:

答案 0 :(得分:1)

这是因为您的WeightsLifted班级没有public no-argument constructor。当Firebase Cloud Firestore SDK对来自数据库的对象进行反序列化时,它要求使用的任何对象都具有我正在讨论的public no-argument constructor。此构造函数可用于实例化类的对象。通过使用公共setter方法或直接访问公共成员来设置对象中的字段。

要解决此问题,只需将以下构造函数添加到您的类中:

public WeightsLifted() {} //Needed for Firebase

你的问题将得到解决。