I'm trying to retrieve the value from the string field group_name in firestore but I get a NullPointerException because the value doesn't get retrieved and added to an arraylist. So the arraylist is empty. I'm trying to get the data within OnCreateView, because I'm working with fragments. Adding data works perfectly (createNewGroup()).
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
db = FirebaseFirestore.getInstance();
//FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder().setPersistenceEnabled(true).setCacheSizeBytes(FirebaseFirestoreSettings.CACHE_SIZE_UNLIMITED).build();
//db.setFirestoreSettings(settings);
}
groupList = new ArrayList<>();
gridView = view.findViewById(R.id.notes_gridview);
//Source source = Source.CACHE;
//https://firebase.google.com/docs/firestore/query-data/get-data#source_options
db.collection("groups").document(key).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
groupList.add(documentSnapshot.getString("group_name"));
Log.w(TAG, "DocumentSnapshot data: " documentSnapshot.getData());
adpter = new GroupAdapter(getActivity(), groupList);
gridView.setAdapter(adpter);
mProgressCircle.setVisibility(View.INVISIBLE);
} else {
Log.w(TAG, "No such document");
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error getting documents: " e.getMessage());
}
});
private void CreateNewGroup(String groupName) {
HashMap<String, String> groupData = new HashMap<>();
groupData.put("group_name", groupName);
HashMap<String, Object> update = new HashMap<>();
update.put(key, groupData);
db.collection("groups").document(key).set(update).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Log.w(TAG, "New group added successfully to Firestore");
((NavigationHost) getActivity()).navigateTo(new MyGroupsFragment(), false);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getActivity(), "Failed adding new group to firestore", Toast.LENGTH_LONG).show();
}
});
}
CodePudding user response:
It looks like the issue is that you have created a document that is more nested than you intended, and includes the document id in the document itself. You created something like this (which you can see in your Firestore screenshot, as well as the log output you posted):
collection[docId] = {docId={group_name=Test}}
when I think you intended to create
collection[docId] = {group_name=Test}
To fix this, you would just change document(key).set(update)
to document(key).set(groupData)
in CreateNewGroup
.
There are several useful examples of how to set and update document data in the Firestore docs.
Alternately, if this is your intended document structure you would need to change how you retrieve group_name
.
Instead of
String group = documentSnapshot.getString("group_name");
you would need something like this to first retrieve the nested map, then get the group_name
attribute from it
String group = documentSnapshot.getData().get(key).get("group_name");