Hy Guys
So I am trying to get this below portion of the code for a shopping list application to run, however i am getting an error that is causing the application to crash.
Code:
public void setProduct(Context context, View shoppingListViewFragment, String userEmail, String userName, ListModel shoppingListModel, ItemModel productModel) {
String listID = shoppingListModel.getListID();
String listName = shoppingListModel.getListName();
String itemID = productModel.getItemID();
String itemName = productModel.getItemName();
Boolean currentList = productModel.getCurrentList();
productNameContainer.setText(itemName);
FirebaseFirestore dbRef = FirebaseFirestore.getInstance();
DocumentReference itemIdRef = dbRef.collection("products").document(listID)
.collection("shoppingListProducts").document(itemID);
itemView.setOnClickListener(view -> {
Map<String, Object> map = new HashMap<>();
if (currentList) {
map.put("currentList", false);
} else {
map.put("currentList", true);
}
itemIdRef.update(map).addOnSuccessListener(aVoid -> {
if (currentList) {
dbRef.collection("shoppingLists").document(userEmail)
.collection("userShoppingLists").document(listID).get().addOnCompleteListener(task -> {
Map<String, Object> map1 = (Map<String, Object>) task.getResult().get("users");
String notificationMessage = userName + " has just bought " + itemName + " from " + listName + "'s list!";
UserNotificationsModel userNotificationsModel = new UserNotificationsModel(notificationMessage, userEmail);
for (Map.Entry<String, Object> entry : map1.entrySet()) {
String sharedUserEmail = entry.getKey();
if (!sharedUserEmail.equals(userEmail)) {
dbRef.collection("notifications").document(sharedUserEmail)
.collection("userNotifications").document()
.set(userNotificationsModel);
}
}
});
}
});
});
itemView.setOnLongClickListener(view -> {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Edit / Delete Item");
EditText editText = new EditText(context);
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
editText.setText(itemName);
editText.setSelection(editText.getText().length());
editText.setHint("Please type item name");
editText.setHintTextColor(Color.GRAY);
builder.setView(editText);
builder.setPositiveButton("Update", (dialogInterface, i) -> {
String newItemName = editText.getText().toString().trim();
Map<String, Object> map = new HashMap<>();
map.put("itemName", newItemName);
itemIdRef.update(map);
});
builder.setNegativeButton("Delete", (dialogInterface, i) -> {
itemIdRef.delete().addOnSuccessListener(aVoid -> Snackbar.make(shoppingListViewFragment, "Item deleted!", Snackbar.LENGTH_LONG).show());
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
return true;
});
}
When the application run, a fatal error occurs at line 61 which is at the for loop " for (Map.Entry entry : map1.entrySet())"
I am getting the below error from Android Studio IDE
E/AndroidRuntime: FATAL EXCEPTION: main Process: example.com.shoppinghelper, PID: 17150 java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Set java.util.Map.entrySet()' on a null object reference at example.com.shoppinghelper.shopping_container.ProductContainer.lambda$null$0$ProductContainer(ProductContainer.java:61) at example.com.shoppinghelper.shopping_container.ProductContainer$$Lambda$6.onComplete(Unknown Source:0) at com.google.android.gms.tasks.zzf.run(Unknown Source:23) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6940) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Basically, the code is trying to data to a notifications collection provided the certain list was shared by someone. However, if if there is no one i shared the particular shopping list with, then i am getting the above stated error.
Video Tutorial which i am following is : https://www.youtube.com/watch?v=6RzB4HXzQyA&list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee&index=16
Please help. :)