Reputation: 2428
I'm trying to delete items in firestore based on the documents time stamp, I'm fairly sure the code is right and I'm definitely sure the timestamps match and yet I get a null pointer
java.lang.NullPointerException: Attempt to invoke virtual method
'com.google.android.gms.tasks.Task
com.google.firebase.firestore.DocumentReference.delete()' on a null object
reference
So here's what I have so far... when the activity starts I run a query and get all the message documents from firestore, I then put these into objects and display them in a recycler view with a custom adapter (basic stuff). What I'm doing now is creating a delete method, so I've created a method in my adapter that adds selected items to a list that I can get to start deleting (getSelectedItems) and I'm using the firestore query below to delete the items
ArrayList<UserMessage> messages = new ArrayList<>();
messages.addAll(mMessageAdapter.getSelectedItems());
final CollectionReference userRef =
db.collection("users")
.document(userId)
.collection("contacts")
.document(myUser.get_id())
.collection("messages");
for (UserMessage message : messages) {
System.out.println(message.getTime_stamp());
//this prints like this- Sat Jun 02 12:20:56 GMT+01:00 2018
Query query = userRef.whereEqualTo("time_stamp",
message.getTime_stamp());
query.get().addOnCompleteListener(new
OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
System.out.println("found an item to delete");
for (DocumentSnapshot doc : task.getResult()) {
doc.getDocumentReference(doc.getId())
.delete()
.addOnSuccessListener(new
OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
System.out.println("deleted an item");
messagesDeleted[0]++;
prog3Message = ("Deleted " +
messagesDeleted[0] + " of " +
allMessages[0] + " messages");
runOnUiThread(changeMessage);
}
});
}
if (messagesDeleted[0] == allMessages[0]) {
prog3.dismiss();
showMenuItems = false;
///selectedMessageList.clear();
hideShowMenu(showMenuItems);
mMessageAdapter.clearSelections();
//System.out.println("all complete ");
}
} else {
}
}
});
}
So to start troubleshooting I added a print statement in the first query (getting all messages) for each document I print the timestamp from both the document and the object (just in case) and it prints out like this
I/System.out: object data Sat Jun 02 12:20:56 GMT+01:00 2018
I/System.out: document data Sat Jun 02 12:20:56 GMT+01:00 2018
I/System.out: object data Sat Jun 02 12:23:16 GMT+01:00 2018
I/System.out: document data Sat Jun 02 12:23:16 GMT+01:00 2018
which as you can see matches a print from the delete method above - Sat Jun 02 12:20:56 GMT+01:00 2018 - and yet i get a null pointer when running the query, one interesting fact is when you go to firestore the timestamp shows like this June 1, 2018 at 5:55:49 PM UTC+1 which is a different format and different region so i tried playing with it a little and ended up doing this (not very elegant)
SimpleDateFormat dateFormat =
new SimpleDateFormat("MMMM dd, yyyy HH:mm:ss aa", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String formatted = dateFormat.format(message.getTime_stamp());
String[] parts = formatted.split(" ");
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < parts.length; i++){
if(i == 2){
stringBuilder.append(parts[i] + " at ");
}else if (i == parts.length -1){
stringBuilder.append(parts[i].toUpperCase() + " UTC+1");
}else{
stringBuilder.append(parts[i] + " ");
}
}
System.out.println(stringBuilder.toString());
//June 02, 2018 at 11:20:56 AM UTC+1
this gives me the right pattern but the time is now an hour out can anyone tell me if I'm trying to reinvent the wheel here hopefully theres a better way
Upvotes: 0
Views: 812
Reputation: 2428
I couldnt figure this out and decided to instead write a message to firestore and then update the item with its id so it reads collection (user) document {userId} collection (contacts) document {contactId} collection (messages) document {messageId} document values id = messageId values message = "blah blah" this way i can delete each item by its id
Upvotes: 1