Gabriel Apostol
Gabriel Apostol

Reputation: 73

Android - Firebase Level of Access

I am new to Android Development and still learning about basics and trying to get an different access I have this structure in my database I'm getting the value of status and using at as user type

Database Structrure

and I got this error

java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.gab.quadrantms.LoginActivity$1$1$1.onDataChange(LoginActivity.java:116)

Login

private DatabaseReference mUserType;
mUserType = FirebaseDatabase.getInstance().getReference().child("Users").child("status");

mUserType.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    // THIS IS WHERE THE ERROR IS POINTING
    String status = dataSnapshot.child("status").getValue().toString();
     if(status.equals("Project Manager"))
     {
        Intent intent = new Intent(LoginActivity.this, Home.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
     }
     else
     {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
     }
  }

Upvotes: 2

Views: 73

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

FirebaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users").child(current_user_id);

ref.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {

      String status = dataSnapshot.child("status").getValue().toString();
      if(status.equals("Project Manager"))
      {
        Intent intent = new Intent(LoginActivity.this, Home.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
      }
      else
      {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
      }
  }

First you need to create a reference to the node Users then since you need to access the child status, and since you have a random id under the node Users. You have to loop inside the direct children of the dataSnapshot(which is in this case the node Users) and you will be able to get the value of status.

Upvotes: 2

Related Questions