0

I am trying to get a selected image uri into my cart database. I have tried to extend an activity from another, but I wasn't sure, and I got an error. here is where I stored all products in ProductActivity.

private void SaveProductInfoToDatabase()
    {
ProductsRef = FirebaseDatabase.getInstance().getReference().child("Products");
...
        HashMap<String, Object> productMap = new HashMap<>();
        productMap.put("description", Description);
        productMap.put("image", downloadImageUrl);
        productMap.put("price", Price);
        productMap.put("pname", Pname);
        

here is what I did to get image in the other activity

.
.
final DatabaseReference cartMap = new HashMap<>();

        cartMap.put("pname", productName.getText().toString());
        cartMap.put("price", productPrice.getText().toString());
       cartMap.put("image", SellerAddNewProductActivity.downloadImageUrl);
}

I got this error in both activities:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference.

Can someone help me please? I am still learning.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • If the error comes from the code you shared, it seems that `productName.getText()` or `productPrice.getText()` is `null`, and you can't call `null.toString()`. A typical way to fix this problem is to check `if (productName.getText() != null) cartMap.put("pname", productName.getText().toString());`, but I recommend reading: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Mar 27 '21 at 14:47
  • my code works completely fine before I extend the activity to get image in the DB. now I am getting errors. are there any alternative on how to get this images in my Cart List database ? – Wafaa Boukli Mar 29 '21 at 12:43

0 Answers0