Reputation: 275
I'm trying to add two new fields Mobile and Message for an existing userid, the user id is not null id is printing on the Log, but when I try to push the value, the app crashes and gives me the following error.
Attempt to invoke virtual method 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)' on a null object reference*
What am I doing wrong?
userphone = (EditText) findViewById(R.id.user_phone);
usermessage = (EditText) findViewById(R.id.user_message);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String phone = userphone.getText().toString().trim();
final String message = usermessage.getText().toString().trim();
String userID = firebaseAuth.getCurrentUser().getUid();
DocumentReference documentReference = fStore.collection("users").document(userID);
Map<String,Object> user = new HashMap<>();
user.put("user_phone",userphone.getText().toString().trim());
user.put("user_message", usermessage.getText().toString().trim());
documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(MainActivity.this, "Successfull", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, "Boooo!", Toast.LENGTH_SHORT).show();
}
});
Upvotes: 0
Views: 479
Reputation: 11
You are not updating the values ; instead you're replacing the prevoius map with a new one. Use fstore.collection().document().update(key,value,key,value); to update specific fields without modifying other values.
Upvotes: 1
Reputation: 317710
fStore
is null. You probably never gave it a value, but we can't see where you defined it. You're going to have to assign it FirebaseFirestore.getInstance()
somewhere.
Upvotes: 3