0

I'm trying to make a rating system for my app but I can only rate once and can't update it.

Error message

The way it works is that when I click 3 stars (out of 5 stars) from the app, I'm expecting that it would pass the value(3) from DBqueries as initialRating unto ProductDetailsActivity where it updates the ratings from Firebase where I can see how many ratings on 1_star, 2_star, 3_star, 4_star and 5_star.

Here's the code from ProductDetailsActivity.java:

Map<String, Object> updateRating = new HashMap<>();
if (DBqueries.myRatedIds.contains(productID)) {
    updateRating.put(initialRating+1+"_star", (long)documentSnapshot.get(initialRating+1+"_star")-1);
    updateRating.put(starPosition + 1 + "_star", (long)documentSnapshot.get(starPosition+1+"_star")+1);
    updateRating.put("average_rating", String.valueOf(calculateAverageRating((long)starPosition+1)));
}

Apparently the line that's causing the error is this:

updateRating.put(initialRating+1+"_star", (long)documentSnapshot.get(initialRating+1+"_star")-1);

The app somehow works if I replace initial rating with a value, e.g.

updateRating.put(initialRating+1+"_star", (long)documentSnapshot.get(1+1+"_star")-1);

Only if the initialRating is used it gives an error.
Here's my DBqueries.java:

if (task.getResult().get("product_ID_" + x).toString().equals(ProductDetailsActivity.productID)) {
    ProductDetailsActivity.initialRating = Integer.parseInt(String.valueOf((long) task.getResult().get("rating_"+x))) - 1;
    if (ProductDetailsActivity.rateContainer != null) {
        ProductDetailsActivity.setRating(ProductDetailsActivity.initialRating);
    }
}

I tried giving initalRating a constant value from the code above to see if it works but it still give the error:

ProductDetailsActivity.initialRating = 2;

I'm still a beginner on Android studio so I really want to understand why am I getting the error. I've been spending my day trying to figure out this problem. I hope you guys can help me. Thanks!

Abra
  • 19,142
  • 7
  • 29
  • 41
David
  • 1
  • 1
  • 1
    Basically you need to look at line 322 and either handle the value being null, or work out why it's unexpectedly null. – Jon Skeet Feb 10 '22 at 08:02
  • Thanks for responding, the line thats causing the error is: 'updateRating.put(initialRating+1+"_star", (long)documentSnapshot.get(initialRating + 1 +"_star")-1);' as initialRating returns null, i cant seem to find the answer why it cannot get the value of initialRating in DBqueries.java. hope you could help me with this – David Feb 10 '22 at 08:20
  • Are you sure that the problem is `initialRating` being null? I suspect it's actually that `documentSnapshot.get(...)` is returning null. But no, we're unlikely to be able to help you work that out without a *lot* more contextual information. Once you understand why you're getting the exception, I suggest you edit the question to reduce it to a minimal example, with all the information needed to understand what's happening, but nothing else. (At the moment we have no idea what's in the document snapshot, for example...) – Jon Skeet Feb 10 '22 at 08:42
  • I appreciate your insight, i tried looking into it and it seems like you're right. Ill update the question if i understand more what's happening. thanks ! – David Feb 10 '22 at 09:05

0 Answers0