android对象引用,Android应用程序中的空对象引用

Hy Guys

所以我试图让下面的部分代码运行购物清单应用程序,但是我收到的错误导致应用程序崩溃 .

码:

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 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 map1 = (Map) task.getResult().get("users");

String notificationMessage = userName + " has just bought " + itemName + " from " + listName + "'s list!";

UserNotificationsModel userNotificationsModel = new UserNotificationsModel(notificationMessage, userEmail);

for (Map.Entry 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 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;

});

}

当应用程序运行时,在第61行出现致命错误,该行位于for循环“for(Map.Entry entry:map1.entrySet())”

我从Android Studio IDE收到以下错误

E / AndroidRuntime:FATAL EXCEPTION:main进程:example.com.shoppinghelper,PID:17150 java.lang.NullPointerException:尝试在null上调用接口方法'java.util.Set java.util.Map.entrySet()' example.com.shoppinghelper.shopping_container.ProductContainer.lambda上的对象引用$ null $ 0 $ ProductContainer(ProductContainer.java:61)at example.com.shoppinghelper.shopping_container.ProductContainer $$ Lambda $ 6.onComplete(Unknown来源:0)at com .google.android.gms.tasks.zzf.run(未知来源:23)在android.os.Handler.handleCallback(Handler.java:789)的android.os.Handler.dispatchMessage(Handler.java:98)在android下载位于android.app.ActivityThread.main(ActivityThread.java:6940)的.os.Looper.loop(Looper.java:164)位于com.android.internal.os的java.lang.reflect.Method.invoke(Native Method) .Zygote $ MethodAndArgsCaller.run(Zygote.java:327)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

基本上,代码正在尝试将数据发送到通知集合,前提是某些列表是由某人共享的 . 但是,如果没有人与我分享特定的购物清单,那么我得到上述错误 .

请帮忙 . :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值