title: Android - Firebase - Getting Child of Child Data
body: <p><strong>Aim</strong></p>
  
  <p>To prompt the data of the current user such as <strong>name</strong>, <strong>address</strong>, <strong>neighbourhood</strong> by acquiring them through the Data Tree within <strong>Firebase Database</strong></p>
  
  <p><strong>Picture of the Data Tree</strong></p>
  
  <p><a href="https://i.stack.imgur.com/Gopjn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gopjn.png" alt="enter image description here"></a></p>
  
  <p><strong>Desrciption</strong></p>
  
  <p>The "London" and "Washington" child within the Data Tree are added in by the Admin account, which means that they are not fixed and that an additional child such as "New York" can be added. </p>
  
  <p>Which would mean that the newly edited Data Tree will have under "Users", there will be "London", "Washington", and "New York"</p>
  
  <p>The other childs, such as those shown below are fixed.</p>
  
  <ul>
  <li><p>Guards</p>
  
  <ul>
  <li>name</li>
  <li>neighbourhood</li>
  </ul></li>
  <li><p>Police</p>
  
  <ul>
  <li>address</li>
  <li>name</li>
  <li>neighbourhood</li>
  </ul></li>
  <li><p>Resident</p>
  
  <ul>
  <li><p>address</p></li>
  <li><p>image</p></li>
  <li><p>name</p></li>
  <li><p>neighbourhood</p></li>
  <li><p>position</p></li>
  <li><p>status</p></li>
  </ul></li>
  </ul>
  
  <p><strong>Problem</strong></p>
  
  <p>I receive a "java.lang.NullPointerException" after attempting to prompt the data of the current user who is logged in. </p>
  
  <p>The error is showing at the code <code>String stgUserHomeName = dataSnapshot.child("Users").getValue().toString();</code>. </p>
  
  <p>I was able to acquire my desired data such as "name", "address" and etc. before but when I had a problem when I added in a <strong>Not-Fixed</strong> parent such as "London" and "Washington".</p>
  
  <p><strong>SettingsActivity Class</strong></p>
  
  <pre><code>import android.content.Intent;
  import android.graphics.Bitmap;
  import android.net.Uri;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ImageView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.google.android.gms.tasks.OnCompleteListener;
  import com.google.android.gms.tasks.Task;
  import com.google.firebase.auth.FirebaseAuth;
  import com.google.firebase.auth.FirebaseUser;
  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;
  import com.google.firebase.storage.FirebaseStorage;
  import com.google.firebase.storage.StorageReference;
  import com.google.firebase.storage.UploadTask;
  import com.squareup.picasso.Callback;
  import com.squareup.picasso.NetworkPolicy;
  import com.squareup.picasso.Picasso;
  
  import java.io.File;
  
  
  public class SettingsActivity extends AppCompatActivity {
  
      private DatabaseReference jSettingsDatabase;
      private FirebaseUser jFirebaseCurrentUser;
  
      private Toolbar jSettingsToolbar;
  
      private ImageView jSettingsImageView;
      private TextView jSettingsDisplayName;
      private TextView jSettingsStatus;
      private TextView jSettingsAddress;
      private TextView jSettingsHomeName;
  
      private Button jSettingsDetailsBtn;
      private Button jSettingsImageBtn;
  
      private static final int jSettingsGallerySelect = 1;
  
      private StorageReference jSettingsStorageReference;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_settings);
  
          jSettingsStorageReference = FirebaseStorage.getInstance().getReference();
  
          jFirebaseCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
  
          String settingsUserID = jFirebaseCurrentUser.getUid();
  
          jSettingsDatabase = FirebaseDatabase.getInstance().getReference().child("Resident").child(settingsUserID);
          jSettingsDatabase.keepSynced(true);
  
          jSettingsImageView = (ImageView) findViewById(R.id.settingUserImg);
  
          jSettingsToolbar = (Toolbar) findViewById(R.id.settingsToolBar);
          setSupportActionBar(jSettingsToolbar);
          getSupportActionBar().setTitle("Settings");
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
  
          jSettingsDisplayName = (TextView) findViewById(R.id.settingUserNameTxt);
          jSettingsStatus = (TextView) findViewById(R.id.settingUserStatusTxt);
          jSettingsAddress = (TextView) findViewById(R.id.settingUserAddressTxt);
          jSettingsHomeName = (TextView) findViewById(R.id.settingsUserHomeTxt);
  
          jSettingsDetailsBtn = (Button) findViewById(R.id.settingChangeDetailsBtn);
          jSettingsImageBtn = (Button) findViewById(R.id.settingChangeImageBtn);
  
          jSettingsDatabase.addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  String stgUserHomeName = dataSnapshot.child("Users").getValue().toString();
                  String stgUserName = dataSnapshot.child("Users").child(stgUserHomeName).child("name").getValue().toString();
                  String stgUserStatus = dataSnapshot.child("Users").child(stgUserHomeName).child("status").getValue().toString();
                  String stgUserHomeAddress = dataSnapshot.child("Users").child(stgUserHomeName).child("address").getValue().toString();
                  final String stgUserImage = dataSnapshot.child("Users").child(stgUserHomeName).child("image").getValue().toString();
  
                  jSettingsDisplayName.setText(stgUserName);
                  jSettingsStatus.setText(stgUserStatus);
                  jSettingsAddress.setText(stgUserHomeAddress);
  
                  if(!stgUserImage.equals("default")){
                      Picasso.with(SettingsActivity.this).load(stgUserImage).networkPolicy(NetworkPolicy.OFFLINE)
                              .placeholder(R.drawable.avataricon).into(jSettingsImageView, new Callback() {
                          @Override
                          public void onSuccess() {
  
                          }
  
                          @Override
                          public void onError() {
                              Picasso.with(SettingsActivity.this).load(stgUserImage).placeholder(R.drawable.avataricon).into(jSettingsImageView);
                          }
                      });
                  }
              }
  
              @Override
              public void onCancelled(DatabaseError databaseError) {
  
              }
          });
  
          jSettingsDetailsBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  String stgUserStatusValue = jSettingsStatus.getText().toString();
                  String stgUserAddressValue = jSettingsAddress.getText().toString();
                  String stgUserNameValue = jSettingsDisplayName.getText().toString();
                  Intent intentDetails = new Intent(SettingsActivity.this, DetailsActivity.class);
                  intentDetails.putExtra("stgUserNameValue" , stgUserNameValue);
                  intentDetails.putExtra("stgUserStatusValue", stgUserStatusValue);
                  intentDetails.putExtra("stgUserAddressValue", stgUserAddressValue);
                  startActivity(intentDetails);
              }
          });
  
          jSettingsImageBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent intentGallery = new Intent();
                  intentGallery.setType("image/*");
                  intentGallery.setAction(Intent.ACTION_GET_CONTENT);
                  startActivityForResult(Intent.createChooser(intentGallery, "SELECT IMAGE"), jSettingsGallerySelect);
              }
          });
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
          if(requestCode == jSettingsGallerySelect &amp;&amp; resultCode == RESULT_OK){
  
              Uri imageUri = data.getData();
  
              String currentUserID = jFirebaseCurrentUser.getUid();
  
              StorageReference imageFilePath = jSettingsStorageReference.child("profileImages").child(currentUserID+".jpg");
  
              imageFilePath.putFile(imageUri).addOnCompleteListener(new OnCompleteListener&lt;UploadTask.TaskSnapshot&gt;(){
                  @Override
                  public void onComplete(@NonNull Task&lt;UploadTask.TaskSnapshot&gt; task) {
                      @SuppressWarnings("VisibleForTests")
                      String downloadImageUrl = task.getResult().getDownloadUrl().toString();
  
                      jSettingsDatabase.child("image").setValue(downloadImageUrl).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
                          @Override
                          public void onComplete(@NonNull Task&lt;Void&gt; task) {
                              if(task.isSuccessful()){
                                  Toast.makeText(SettingsActivity.this, "Upload Successful", Toast.LENGTH_SHORT).show();
                              }else{
                                  Toast.makeText(SettingsActivity.this, "Upload Failed", Toast.LENGTH_SHORT).show();
                              }
                          }
                      });
                  }
              });
          }
      }
  }
  </code></pre>
  
  <p><strong>Code Explanation</strong></p>
  
  <p>The <code>jSettingsHomeName</code> and <code>stgUserHomeName</code> are meant to be the "London", "Washington", and etc. </p>
  
  <p><strong>Solution to almost Similar Problem</strong></p>
  
  <p>In the link: <a href="https://stackoverflow.com/questions/43293935/how-to-get-child-of-child-value-from-firebase-in-android">How to get child of child value from firebase in android?</a> , it shows how a programmer can get the child of a child but the reason as to why I am unable to follow the is because my "London" and "Washington" child tier <strong>isn't fixed</strong></p>
  
  <p><strong>Update: Trying Solution given by Ewald B.</strong></p>
  
  <pre><code>jSettingsDatabase.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
          for(DataSnapshot node : dataSnapshot.getChildren()) {
              String stgUserHomeName = node.getKey();
              String stgUserName = node.child("Resident").child(settingsUserID).child("name").getValue().toString();
              String stgUserStatus = node.child("Resident").child(settingsUserID).child("status").getValue().toString();
              String stgUserHomeAddress = node.child("Resident").child(settingsUserID).child("address").getValue().toString();
              final String stgUserImage = node.child("Resident").child(settingsUserID).child("image").getValue().toString();
  
              jSettingsHomeName.setText(stgUserHomeName);
              jSettingsDisplayName.setText(stgUserName);
              jSettingsStatus.setText(stgUserStatus);
              jSettingsAddress.setText(stgUserHomeAddress);
  
              if(!stgUserImage.equals("default")){
                  Picasso.with(SettingsActivity.this).load(stgUserImage).networkPolicy(NetworkPolicy.OFFLINE)
                          .placeholder(R.drawable.avataricon).into(jSettingsImageView, new Callback() {
                      @Override
                      public void onSuccess() {
  
                      }
  
                      @Override
                      public void onError() {
                          Picasso.with(SettingsActivity.this).load(stgUserImage).placeholder(R.drawable.avataricon).into(jSettingsImageView);
                      }
                  });
              }
          }
  
      }
  
      @Override
      public void onCancelled(DatabaseError databaseError) {
  
      }
  });
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <p>java.lang.NullPointerException at com.example.task.app.SettingsActivity$1.onDataChange(SettingsActivity.java:91)</p>
  
  <p>Line 91 points to <code>String stgUserName = node.child("Resident").child(settingsUserID).child("name").getValue().toString();</code></p>