How to get logged user's information in drawer menu?

Theo0201 :

I've tried to retrieve the current user's username and email from firebase and display it in the drawer menu. However, the program crashed and return the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.android.myapplication.NavigationActivity$1.onDataChange(NavigationActivity.java:79)

Here is my Navigation Activity

public class NavigationActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    protected DrawerLayout drawer;
    private String userId;
    private TextView email,name;
    private DatabaseReference mDatabase;
    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView=findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        name = (TextView)findViewById(R.id.name);
        email=(TextView)findViewById(R.id.email);

        mAuth=FirebaseAuth.getInstance();
        userId= mAuth.getCurrentUser().getUid();

        getUserInformation();

    }

    private void getUserInformation() {
        DatabaseReference userDatabase= FirebaseDatabase.getInstance().getReference().child("users").child(userId);

        userDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    for (DataSnapshot child : dataSnapshot.getChildren()) {
                        if (child.getKey().equals("name")) {//find the child that Equals to start
                            name.setText(child.getValue().toString());
                        }
                        if (child.getKey().equals("email")) {//find the child that Equals to start
                            email.setText(child.getValue().toString());
                        }
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }


    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()){
            case R.id.nav_run:
                Intent intent = new Intent(NavigationActivity.this,running_map.class);
                startActivity(intent);
                finish();
                break;
            case R.id.nav_history:
                Intent intent2 = new Intent(NavigationActivity.this,HistoryActivity.class);
                startActivity(intent2);
                finish();
                break;
            //......
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Here is my database: enter image description here I guessed that this is because the NavigationActivity was not the main activity, however, even when I changed it to the main activity, I still got the same error.

Peter Haddad :

You dont need the for loop:

    private void getUserInformation() {
        DatabaseReference userDatabase= FirebaseDatabase.getInstance().getReference().child("users").child(userId);

        userDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                        if (dataSnapshot.child("name").getValue(String.class).equals("name")) {//find the child that Equals to start
                            name.setText(child.getValue().toString());
                        }
                        if (dataSnapshot.child("email").getValue(String.class).equals("email")) {//find the child that Equals to start
                            email.setText(child.getValue().toString());
                        }
                    }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

Your reference is at node userId, therefore to get the details you dont need to iterate.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291471&siteId=1