title: Android: Trying to fetch the push() key id and extract the rating value from the child element
body: <p>I am making a feedback app where the customer has to submit a rating (1-5) and that data pushes onto Firebase realtime database. The database in Firebase which looks like this -</p>
  <pre><code>{
    &quot;Users&quot; : {
      &quot;-MCHoOShwgxPE3u2drjz&quot; : {
        &quot;Arpit Mundra&quot; : {
          &quot;name&quot; : &quot;Arpit Mundra&quot;,
          &quot;rating&quot; : 5
        }
      },
      &quot;-MCHqbF4UvX02OYJNuPv&quot; : {
        &quot;Ankit Mundra&quot; : {
          &quot;name&quot; : &quot;Ankit Mundra&quot;,
          &quot;rating&quot; : 5
        }
      },
      &quot;-MCHr-q_amx1vBJkyq2S&quot; : {
        &quot;Harsh Chauhan&quot; : {
          &quot;name&quot; : &quot;Harsh Chauhan&quot;,
          &quot;rating&quot; : 5
        }
      }
    }
  }
  </code></pre>
  <p>Now I want to calculated the average of the <code>rating</code> of all the users which falls under user which again falls under the push() id. My question is how do I fetch the unique push() id to extract the rating value from it?</p>
  <p>Here is the code -</p>
  <pre><code>package com.example.feedback;
  import android.os.Bundle;
  import android.view.Window;
  import android.view.WindowManager;
  import android.widget.TextView;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.StringRes;
  import androidx.appcompat.app.AppCompatActivity;
  
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.database.FirebaseDatabase;
  import com.google.firebase.database.ValueEventListener;
  import com.google.android.gms.tasks.OnCompleteListener;
  import com.google.android.gms.tasks.OnFailureListener;
  import com.google.android.gms.tasks.OnSuccessListener;
  import com.google.android.gms.tasks.Task;
  import com.google.firebase.database.DataSnapshot;
  import com.google.firebase.database.DatabaseError;
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.database.FirebaseDatabase;
  import com.google.firebase.database.ValueEventListener;
  
  public class Score extends AppCompatActivity {
  
      TextView avgScore;
      DatabaseReference dbRef;
      String mGroupID
  
      
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
          getSupportActionBar().hide(); // hide the title bar
          this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
          setContentView(R.layout.score);
          avgScore = findViewById(R.id.textView2);
          dbRef = FirebaseDatabase.getInstance().getReference().child(mGroupID);
          mGroupID = dbRef.push().getKey();
  
          scoreRealTime();
      }
  
      public void scoreRealTime() {
          dbRef.addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(@NonNull DataSnapshot snapshot) {
                  double total = 0;
                  for (DataSnapshot ds : snapshot.getChildren()){
                      double values = Double.parseDouble(ds.child(&quot;rating&quot;).getValue().toString());
                      total = total + values;
                  }
                  double average = (double) total / snapshot.getChildrenCount();
                  avgScore.setText(String.format(&quot;%.2f&quot;, average));
              }
  
              @Override
              public void onCancelled(@NonNull DatabaseError error) {
              }
          });
      }
  }
  </code></pre>
  <p>It is throwing a NullPointerException error at String <code>double values = Double.parseDouble(ds.child(&quot;rating&quot;).getValue().toString());</code> line.</p>
  <p>Thanks for any help in advance.</p>
  <p>EDIT - MainActivity.java -</p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      EditText name;
      ImageView oneStar, twoStar, threeStar, fourStar, fiveStar;
      Intent intent;
      FirebaseDatabase rootNode;
      DatabaseReference reference;
  
      public void displayScore() {
          intent = new Intent(getApplicationContext(), Score.class);
          startActivity(intent);
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
          getSupportActionBar().hide(); // hide the title bar
          this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
          setContentView(R.layout.activity_main);
          name =  findViewById(R.id.editTextPersonName);
          oneStar =  findViewById(R.id.imageView1);
          twoStar =  findViewById(R.id.imageView2);
          threeStar = findViewById(R.id.imageView3);
          fourStar =  findViewById(R.id.imageView4);
          fiveStar =  findViewById(R.id.imageView5);
          oneStar.setTag(1);
          twoStar.setTag(2);
          threeStar.setTag(3);
          fourStar.setTag(4);
          fiveStar.setTag(5);
  
          oneStar.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if (name.length() == 0){
                      name.setError(&quot;Please enter your full name.&quot;);
                  }
                  else {
                      name.setError(null);
                      rootNode = FirebaseDatabase.getInstance();
                      reference = rootNode.getReference().child(&quot;Users&quot;);
                      //Fetch all values
                      String username = name.getText().toString();
                      String value = view.getTag().toString();
                      int rating = Integer.parseInt(value);
                      UserHelper helper = new UserHelper(username,rating);
                      reference.push().child(username).setValue(helper);
                      Toast.makeText(MainActivity.this, &quot;Feedback submitted successfully!&quot;, Toast.LENGTH_SHORT).show();
                      sleep(2500);
                      displayScore();
                  }
              }
          });
  </code></pre>