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();
                   }
                });
            }
            }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Israt
  • 87
  • 1
  • 9

0 Answers0