0

I'm going to calculate the average rating of books.

Here,Rating and Counter is initialized in Firebase. I am getting fatal error on this line of my code sum = Double.parseDouble(r); which is

java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference

But I could not find out the reason. It accurately retrieves both the counter value and rating without the rating calculation portion.

Here's the code and the database snapshot.

enter image description here

 public class GetReview { private FirebaseUser user; private Firebase mrootref; private String path2="hhhh"; private String c; private String r; private Double sum; // private String rate = " "; private Double avg; private Double counter; private Double rate; private double rounded; GetReview() { user = FirebaseAuth.getInstance().getCurrentUser(); Log.d("path1", path2); path2= myCategory.getPath(); Log.d("paths", path2); }; public void setReview(String bookname, Double rating) { rate= rating; //sum = sum+rate; Log.d("sum?", rating.toString()); path2 += "/"+bookname+"/"; mrootref = new Firebase(path2); mrootref.addValueEventListener(new com.firebase.client.ValueEventListener() { @Override public void onDataChange(com.firebase.client.DataSnapshot dataSnapshot) { for (com.firebase.client.DataSnapshot mshot : dataSnapshot.getChildren()) { Log.d("emmaList ", mshot.toString()); //Map<String, String> mmap = mshot.getValue(Map.class); // c = mshot.child("Counter:").getValue().toString(); //String review = mshot.getKey().toString(); //String emma = mshot.getKey().toString(); //Log.d(review, "I am the key"); if (mshot.getKey().toString().equals("Counter")) { c =String.valueOf( mshot.getValue()); Log.d(c, "I am counter"); } if (mshot.getKey().toString().equals("Rating")) { r =String.valueOf( mshot.getValue()); Log.d(r, "I am rate"); } //r = mmap.get("Rating"); counter = Double.parseDouble(c); counter++; sum = Double.parseDouble(r); sum = sum + rate; avg = sum / counter; sum = avg; Log.d(counter.toString(), "i am review"); //Log.d( avg.toString(),"i am avg"); BigDecimal bd = new BigDecimal(avg); bd = bd.round(new MathContext(3)); rounded = bd.doubleValue(); //Map revBook = new HashMap(); mrootref.child("Counter").setValue(counter.toString()); mrootref.child("Rating").setValue(Double.toString(rounded)); } } @Override public void onCancelled(FirebaseError firebaseError) { //Toast.makeText(GetReview.this, "Error in data retrieval", Toast.LENGTH_SHORT).show(); } }); } } 
4
  • what it prints.. Log.d("emmaList ", mshot.toString());. cuz your r variable's value is null. Commented Nov 18, 2016 at 9:02
  • It only prints the counter value D/emmaList: DataSnapshot { key = Counter, value = 0 } But no Rating value. Commented Nov 18, 2016 at 9:09
  • actually you are looping over data nodes of list.. try to put the code(that you wrote inside after the if statements) outside of for loop.. Commented Nov 18, 2016 at 9:25
  • It shows garbage value Commented Nov 18, 2016 at 9:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.