1

What I am trying to do?

Access to Firebase Firestore data from some collection, attach it to a class to make a simple math operation.

What's my problem?

I am getting this error:

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)

Code?

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; }
}

As you can see the error I'm getting is in this line:

wl = documentSnapshot.toObject(WeightsLifted.class);

I read the docs and I saw some example implemented that way but they don't explain how to create the class to use it for attach the data from the Firestore within it.

I have sought within the web and found no solution for my problem. So, I am stucked. Any help is so much appreciated, please.

Thanks in advance.

  • [What is a NullPointerException and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Krease Mar 16 '18 at 21:07

1 Answers1

4

This is happening because your WeightsLifted class does not have the public no-argument constructor. When the Firebase Cloud Firestore SDK deserializes objects coming from the database, it requires that any objects in use to have this public no-argument constructor that I was talking about. This constructor can be used to instantiate the object of the class. Fields in the objects are set by using public setter methods or direct access to public members.

To solve this, just add the following constructor to your class:

public WeightsLifted() {} //Needed for Firebase

And your problem will be solved.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193