title: Attempt to invoke virtual method 'java.lang.Long com.google.firebase.firestore.DocumentSnapshot.getLong(java.lang.String)' on a null object reference
body: <p>Please help me </p>
  
  <pre><code>    public class ProductDetailsActivity extends AppCompatActivity {
  
      public static  boolean running_wishlist_query = false;
      public static  boolean running_rating_query = false;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_product_details);
          Toolbar toolbar = findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
          getSupportActionBar().setDisplayShowTitleEnabled(false);
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
  
          productID = getIntent().getStringExtra("PRODUCT_ID");
  
  
  
          inititalRating = -1;
  
  
          firebaseFirestore = FirebaseFirestore.getInstance();
  
          final List&lt;String&gt; productImages = new ArrayList&lt;&gt;();
  
  
          firebaseFirestore.collection("PRODUCTS").document(productID)
                  .get().addOnCompleteListener(new OnCompleteListener&lt;DocumentSnapshot&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;DocumentSnapshot&gt; task) {
                          if (task.isSuccessful()) {
                              documentSnapshot = task.getResult();
  
                              for (long x = 1; x &lt; documentSnapshot.getLong("no_of_product_images") + 1; x++) {
                                  productImages.add(documentSnapshot.getString("product_image_" + x));
                              }
                              ProductImagesAdapter productImagesAdapter = new ProductImagesAdapter(productImages);
                              productImagesViewPager.setAdapter(productImagesAdapter);
  
                              productTitle.setText(documentSnapshot.getString("product_title"));
                              averageRatingMiniView.setText(String.valueOf(documentSnapshot.get("average_rating")));
                              totalRatingMiniView.setText("(" + documentSnapshot.getLong("total_ratings") + ")ratings");
                              productPrice.setText("TK." + documentSnapshot.getString("product_price") + "/-");
                              cuttedPrice.setText("TK." + documentSnapshot.getString("cutted_price") + "/-");
                              if (documentSnapshot.getBoolean("COD")) {
                                  codIndicator.setVisibility(View.VISIBLE);
                                  tvCodIndicator.setVisibility(View.VISIBLE);
                              } else {
                                  codIndicator.setVisibility(View.INVISIBLE);
                                  tvCodIndicator.setVisibility(View.INVISIBLE);
                              }
                              rewardTitle.setText(documentSnapshot.getLong("free_coupens") + documentSnapshot.getString("free_coupen_title"));
                              rewardBody.setText(documentSnapshot.getString("free_coupen_body"));
                              if (documentSnapshot.getBoolean("use_tab_layout")) {
                                  productDetailsTabsContainer.setVisibility(View.VISIBLE);
                                  productDetailsOnlyContainer.setVisibility(View.GONE);
                                  productDescription = documentSnapshot.getString("product_description");
                                  productOtherDetails = documentSnapshot.getString("product_other_details");
  
                                  for (long x = 1; x &lt; documentSnapshot.getLong("total_spec_titles") + 1; x++) {
                                      productSpecificationModelList.add(new ProductSpecificationModel(0, documentSnapshot.getString("spec_title_" + x)));
                                      for (long y = 1; y &lt; documentSnapshot.getLong("spec_title_" + x + "_total_fields") + 1; y++) {
                                          productSpecificationModelList.add(new ProductSpecificationModel(1, documentSnapshot.getString("spec_title_" + x + "_field_" + y + "_name"),
                                                  documentSnapshot.getString("spec_title_" + x + "_field_" + y + "_value")));
                                      }
                                  }
                              } else {
                                  productDetailsTabsContainer.setVisibility(View.GONE);
                                  productDetailsOnlyContainer.setVisibility(View.GONE);
                                  productOnlyDescriptionBody.setText(documentSnapshot.getString("product_description"));
                              }
                              totalRatings.setText(documentSnapshot.getLong("total_ratings") + " ratings");
  
                              for (int x = 0; x &lt; 5; x++) {
                                  TextView rating = (TextView) ratingsNumberContainer.getChildAt(x);
                                  rating.setText(String.valueOf(documentSnapshot.getLong((5 - x) + "_star")));
  
                                  ProgressBar progressBar = (ProgressBar) ratingsProgressbarContainer.getChildAt(x);
                                  int maxProgress = Integer.parseInt(String.valueOf(documentSnapshot.getLong("total_ratings")));
                                  progressBar.setMax(maxProgress);
                                  progressBar.setProgress(Integer.parseInt(String.valueOf(documentSnapshot.getLong((5 - x) + "_star"))));
  
                              }
                              totalRatingsFigure.setText(String.valueOf(documentSnapshot.getLong("total_ratings")));
                              averageRating.setText(String.valueOf(documentSnapshot.get("average_rating")));
                              productDetailsViewPager.setAdapter(new ProductDetailsAdapter(getSupportFragmentManager(), productDescription, productOtherDetails, productSpecificationModelList, productDetailsTabLayout.getTabCount()));
  
                              if(currentUser != null) {
                                  if(DBqueries.myRating.size() == 0){
                                          DBqueries.loadRatingList(ProductDetailsActivity.this);
                                  }
                                  if (DBqueries.wishList.size() == 0) {
                                      DBqueries.loadWishList(ProductDetailsActivity.this, loadingDialog,false);
                                  } else {
                                      loadingDialog.dismiss();
                                  }
                              }else{
                                  loadingDialog.dismiss();
                              }
  
                              if(DBqueries.myRatedIds.contains(productID)){
                                  int index = DBqueries.myRatedIds.indexOf(productID);
                                  inititalRating = Integer.parseInt(String.valueOf(DBqueries.myRating.get(index)));
                                  setRating(inititalRating);
                              }
  
                              if(DBqueries.wishList.contains(productID)){
                                  ALREADY_ADDED_TO_WISHLIST = true;
                                  addToWishlistBtn.setSupportImageTintList(ColorStateList.valueOf(getResources().getColor(R.color.colorPrimary)));
                              }else {
                                  addToWishlistBtn.setSupportImageTintList(ColorStateList.valueOf(Color.parseColor("#9e9e9e")));
                                  ALREADY_ADDED_TO_WISHLIST = false;
                              }
  
                          } else {
                              loadingDialog.dismiss();
                              String error = task.getException().getMessage();
                              Toast.makeText(ProductDetailsActivity.this, error, Toast.LENGTH_SHORT).show();
                          }
                      }
                  });
  
  
          //////rating layout
          rateNowContainer = findViewById(R.id.rate_now_container);
          for(int x = 0; x&lt; 6;x++){
              rateNowContainer.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                  @Override
                  public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                      final int starPosition = (int) ratingBar.getRating();
                      if(currentUser == null){
                          signInDialog.show();
                      }else {
                          if(!running_rating_query){
                              running_rating_query = true;
                              setRating(starPosition);
                              Map&lt;String, Object&gt; updateRating = new HashMap&lt;&gt;();
                              //int initialTestRating  = Integer.parseInt(String.valueOf(documentSnapshot.getLong("rating_" + finalX)));
                              if(DBqueries.myRatedIds.contains(productID)){
  
                                  updateRating.put(inititalRating+"_star", documentSnapshot.getLong(inititalRating+"_star") - 1); // -1
                                  updateRating.put(starPosition+"_star", documentSnapshot.getLong(starPosition+"_star")+ 1); // +1
                                  updateRating.put("average_rating",String.valueOf(calculateAverageRating()));
  
                              }else {
                                  updateRating.put(starPosition+ "_star", documentSnapshot.getLong(starPosition+ "_star")+1);
                                  updateRating.put("total_ratings", documentSnapshot.getLong("total_ratings")+1);
                                  updateRating.put("average_rating", String.valueOf(calculateAverageRating()));
  
                              }
  
                              firebaseFirestore.collection("PRODUCTS").document(productID)
                                      .update(updateRating).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
                                  @Override
                                  public void onComplete(@NonNull Task&lt;Void&gt; task) {
                                      if (task.isSuccessful()) {
  
  
                                          Map&lt;String, Object&gt; rating = new HashMap&lt;&gt;();
                                          if(DBqueries.myRatedIds.contains(productID)){
                                              rating.put("rating_"+DBqueries.myRatedIds.indexOf(productID),starPosition);
                                          }else {
                                              rating.put("list_size", DBqueries.myRatedIds.size() + 1);
                                              rating.put("product_ID_" + DBqueries.myRatedIds.size(), productID);
                                              rating.put("rating_" + DBqueries.myRatedIds.size(), (long) starPosition);
                                          }
  
                                          firebaseFirestore.collection("USERS").document(currentUser.getUid()).collection("USER_DATA").document("MY_RATINGS")
                                                  .update(rating).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
                                              @Override
                                              public void onComplete(@NonNull Task&lt;Void&gt; task) {
                                                  if (task.isSuccessful()) {
  
                                                      if(DBqueries.myRatedIds.contains(productID)){
  
                                                          DBqueries.myRating.set(DBqueries.myRatedIds.indexOf(productID), (long) starPosition);
  
                                                          TextView oldRating = (TextView) ratingsNumberContainer.getChildAt(5 - inititalRating);
                                                          TextView finalRating = (TextView) ratingsNumberContainer.getChildAt(5 - starPosition);
  
                                                          oldRating.setText(String.valueOf(Integer.parseInt(oldRating.getText().toString())-1));
                                                          finalRating.setText(String.valueOf(Integer.parseInt(finalRating.getText().toString()) + 1));
  
  
  
                                                      }else {
                                                          DBqueries.myRatedIds.add(productID);
                                                          DBqueries.myRating.add((long) starPosition);
  
                                                          TextView rating = (TextView) ratingsNumberContainer.getChildAt(5 - starPosition);
                                                          rating.setText(String.valueOf(Integer.parseInt(rating.getText().toString()) + 1));
  
  
                                                          totalRatingMiniView.setText("(" + (documentSnapshot.getLong("total_ratings")) + ")ratings");
                                                          totalRatings.setText(documentSnapshot.getLong("total_ratings") + " ratings");
                                                          totalRatingsFigure.setText(String.valueOf(documentSnapshot.getLong("total_ratings")));
  
  
  
                                                          Toast.makeText(ProductDetailsActivity.this, "Thank you ! for rating", Toast.LENGTH_SHORT).show();
                                                      }
  
                                                      averageRating.setText(String.valueOf(calculateAverageRating()));
                                                      averageRatingMiniView.setText(String.valueOf(calculateAverageRating()));
  
                                                      for (int x = 0; x &lt; 5; x++) {
                                                          TextView ratingfigures = (TextView) ratingsNumberContainer.getChildAt(x);
  
                                                          ProgressBar progressBar = (ProgressBar) ratingsProgressbarContainer.getChildAt(x);
                                                          if(!DBqueries.myRatedIds.contains(productID)) {
                                                              int maxProgress = Integer.parseInt(String.valueOf(documentSnapshot.getLong("total_ratings")));
                                                              progressBar.setMax(maxProgress);
                                                          }
                                                          progressBar.setProgress(Integer.parseInt(ratingfigures.getText().toString()));
  
                                                      }
  
  
                                                  } else {
                                                      setRating(inititalRating);
                                                      String error = task.getException().getMessage();
                                                      Toast.makeText(ProductDetailsActivity.this, error, Toast.LENGTH_SHORT).show();
                                                  }
                                                  running_rating_query = false;
                                              }
                                          });
  
                                      } else {
                                          running_rating_query = false;
                                          setRating(inititalRating);
                                          String error = task.getException().getMessage();
                                          Toast.makeText(ProductDetailsActivity.this, error, Toast.LENGTH_SHORT).show();
                                      }
                                  }
                              });
  
                          }
                      }
                  }
              });
          }
          //////rating layout
  
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          currentUser = FirebaseAuth.getInstance().getCurrentUser();
          if(currentUser == null){
              coupenRedemptionLayout.setVisibility(View.GONE);
          }else {
              coupenRedemptionLayout.setVisibility(View.VISIBLE);
          }
  
          if(currentUser != null) {
              if(DBqueries.myRating.size() == 0){
                  DBqueries.loadRatingList(ProductDetailsActivity.this);
              }
              if (DBqueries.wishList.size() == 0) {
                  DBqueries.loadWishList(ProductDetailsActivity.this, loadingDialog,false);
              } else {
                  loadingDialog.dismiss();
              }
          }else{
              loadingDialog.dismiss();
          }
  
          if(DBqueries.myRatedIds.contains(productID)){
              int index = DBqueries.myRatedIds.indexOf(productID);
              inititalRating = Integer.parseInt(String.valueOf(DBqueries.myRating.get(index)));
              setRating(inititalRating);
          }
  
          if(DBqueries.wishList.contains(productID)){
              ALREADY_ADDED_TO_WISHLIST = true;
              addToWishlistBtn.setSupportImageTintList(getResources().getColorStateList(R.color.colorPrimary));
          }else{
              ALREADY_ADDED_TO_WISHLIST = false;
              addToWishlistBtn.setSupportImageTintList(ColorStateList.valueOf(Color.parseColor("#9e9e9e")));
          }
  
      }
  
  
      public static void setRating(int starPosition) {
          rateNowContainer.setRating(starPosition);
  
      }
  
      private float calculateAverageRating(){
          float totalStars = documentSnapshot.getLong("1_star")*1 +
                  documentSnapshot.getLong("2_star")*2 +
                  documentSnapshot.getLong("3_star")*3 +
                  documentSnapshot.getLong("4_star")*4 +
  
          if(documentSnapshot.getLong("total_ratings") != null &amp;&amp; totalStars != 0){
              return totalStars / documentSnapshot.getLong("total_ratings");
          }else {
              return 0;
          }
      }   
  }
  </code></pre>
  
  <p>This is error</p>
  
  <p>E/AndroidRuntime: FATAL EXCEPTION: main
          Process: com.gabwire.mymall, PID: 3985
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gabwire.mymall/com.gabwire.mymall.ProductDetailsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Long com.google.firebase.firestore.DocumentSnapshot.getLong(java.lang.String)' on a null object reference
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
          at android.app.ActivityThread.access$800(ActivityThread.java:153)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5305)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Long com.google.firebase.firestore.DocumentSnapshot.getLong(java.lang.String)' on a null object reference
          at com.gabwire.mymall.ProductDetailsActivity$4.onRatingChanged(ProductDetailsActivity.java:372)
          at android.widget.RatingBar.dispatchRatingChange(RatingBar.java:319)
          at android.widget.RatingBar.onProgressRefresh(RatingBar.java:262)
          at android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:1262)
          at android.widget.ProgressBar.refreshProgress(ProgressBar.java:1274)
          at android.widget.ProgressBar.setProgress(ProgressBar.java:1321)
          at android.widget.ProgressBar.setProgress(ProgressBar.java:1302)
          at android.widget.RatingBar.setRating(RatingBar.java:198)
          at com.gabwire.mymall.ProductDetailsActivity.setRating(ProductDetailsActivity.java:633)
          at com.gabwire.mymall.ProductDetailsActivity.onStart(ProductDetailsActivity.java:609)
          at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
          at android.app.Activity.performStart(Activity.java:6006)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2290)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
          at android.app.ActivityThread.access$800(ActivityThread.java:153) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305) 
          at android.os.Handler.dispatchMessage(Handler.java:102) 
          at android.os.Looper.loop(Looper.java:135) 
          at android.app.ActivityThread.main(ActivityThread.java:5305) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at java.lang.reflect.Method.invoke(Method.java:372) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
  I/Process: Sending signal. PID: 3985 SIG: 9</p>