title: Loading profile image and username in the navigation bar from firebase
body: <p><a href="https://i.stack.imgur.com/6fHYB.png" rel="nofollow noreferrer">image of database structure</a></p>
  
  <p><a href="https://i.stack.imgur.com/Px6HK.png" rel="nofollow noreferrer">code for database structure</a></p>
  
  <p>I am trying to retrieve the profile image and username that is saved by the user during the registration process in the navigation header. Here is something that I did . At first I tried to get string <strong>current user</strong>  but it showed me null pointer exception then I tried with if statement and changed the child from <strong>current user id</strong> to <strong>Users</strong> , it only solved the null pointer but this didn't retrive the data from firebase.</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>   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.squareup.picasso.Picasso;
  
  import de.hdodenhof.circleimageview.CircleImageView;
  
  public class MainActivity extends AppCompatActivity {
      private NavigationView navigationView;
      private DrawerLayout drawerLayout;
      private RecyclerView postlist;
      private Toolbar mToolbar;
      private ActionBarDrawerToggle actionBarDrawerToggle;
      private FirebaseAuth mAuth;
      private FirebaseUser FirebaseUser;
      private DatabaseReference  UsersRef;
      private CircleImageView NavProfileImage;
      private TextView NavProfileUserName;
      String currentUserID;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          FirebaseUser mFirebaseUser = mAuth.getCurrentUser();
          if (mFirebaseUser != null) {
              currentUserID = mFirebaseUser.getUid();
          }
          mAuth = FirebaseAuth.getInstance();
  
  
  
          mToolbar =(Toolbar) findViewById(R.id.main_page_toolbar);
          setSupportActionBar(mToolbar);
          getSupportActionBar().setTitle("Home");
  
          drawerLayout = (DrawerLayout) findViewById(R.id.drawable_layout);
         actionBarDrawerToggle =  new ActionBarDrawerToggle(MainActivity.this,drawerLayout,R.string.drawer_open, R.string.drawer_close);
          navigationView = (NavigationView)findViewById(R.id.navigation_view);
         drawerLayout.addDrawerListener(actionBarDrawerToggle);
         actionBarDrawerToggle.syncState();
         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
  
          View navView = navigationView.inflateHeaderView(R.layout.nav_header);
          NavProfileImage = (CircleImageView)navView.findViewById(R.id.nav_profile_image);
          NavProfileUserName = (TextView) navView.findViewById(R.id.nav_user_full_name);
  
          UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
          UsersRef.child(currentUserID).addValueEventListener(new ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot)
              {
                  if(dataSnapshot.exists())
                  {   if (dataSnapshot.hasChild("fullname")){
                      String fullname = dataSnapshot.child("fullname").getValue().toString();
                      NavProfileUserName.setText(fullname);
                  }if (dataSnapshot.hasChild("profileimages")) {
                      String image = dataSnapshot.child("profileimages").getValue().toString();
                      Picasso.with(MainActivity.this).load(image).placeholder(R.drawable.profile).into(NavProfileImage);
  
                  }else {
                      Toast.makeText(MainActivity.this, "Profile name do not exists...", Toast.LENGTH_SHORT).show();
                  }
  
  
  
  
                  }
              }
  
              @Override
              public void onCancelled(DatabaseError databaseError) {
  
              }
          });
  
          navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
              @Override
              public boolean onNavigationItemSelected(@NonNull MenuItem item)
              { UserMenuSelector(item);
  
                  return false;
              }
          });
      }
  
      @Override
      protected void onStart() {
          super.onStart();
          FirebaseUser currentUser = mAuth.getCurrentUser();
          if (currentUser == null)
          {
              sendUserToLoginActivity();
          }else{
              CheckUserExistance();
          }
      }
  
      private void CheckUserExistance()
      {
          final String current_user_id = mAuth.getCurrentUser().getUid();
            UsersRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (!dataSnapshot.hasChild(current_user_id)){
                        sendUserToSetupActivity();
                    }
                }
  
                @Override
                public void onCancelled(DatabaseError databaseError) {
  
                }
            });
      }
  
      private void sendUserToSetupActivity() {
          Intent setupIntent = new Intent(MainActivity.this, SetupActivity.class);
          setupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
          startActivity(setupIntent);
          finish();
  
      }
  
      private void sendUserToLoginActivity()
      {
          Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
           loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
           startActivity(loginIntent);
           finish();
  
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          if (actionBarDrawerToggle.onOptionsItemSelected(item))
          {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      private void UserMenuSelector(MenuItem item)
      {
          switch (item.getItemId())
          {
              case R.id.nav_home:
                  Toast.makeText(this,"Home",Toast.LENGTH_SHORT).show();
                  break;
              case R.id.nav_post:
                  break;
              case R.id.nav_Profile:
                  Toast.makeText(this,"profile",Toast.LENGTH_SHORT).show();
                  break;
              case R.id.nav_find_friends:
                  Toast.makeText(this,"frns",Toast.LENGTH_SHORT).show();
                  break;
              case R.id.nav_logout:
                  mAuth.signOut();
                  sendUserToLoginActivity();
                  break;
          }
      }
  }
  </code></pre>