0

I am trying to show user information stored in my Firebase Realtime Database to text views on a separate page but when I run, LOGCAT is is throwing the error below. I am retrieving the unique id that is stored for the user but Im guessing it is not pulling the data.

This is for a new android studio application that takes a users information stored it in the database and retrieves to a profile pulling all of the information like name, email, school, etc. I have tried changing the object of user and the verification of the user to to authentication check but to no avail. I'm guessing it's a simple fix but I am not seeing it. The error is here.

public class ProfileActivity extends AppCompatActivity {

    //Firebase and Database Stuff
    private FirebaseAuth mAuth;
    private FirebaseUser user; // saying it is never assigned
    private TextView Email;
    private TextView TwoPNum;
private TextView Meal;
private TextView PantherFunds;
private TextView Expiration;
private TextView Campus;
private Button logout;

private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;


// TAG
private static final String TAG = "ProfileActivity";


//declare the database reference object. This is what we use to access the database.


// KEEP AN EYE ON
private String userID = user.getUid();

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

    Email = (TextView) findViewById(R.id.profileEmail);
    TwoPNum = (TextView) findViewById(R.id.profileUid);
    Meal = (TextView) findViewById(R.id.mealsNum);
    PantherFunds = (TextView) findViewById(R.id.pFundsNum);
    Expiration = (TextView) findViewById(R.id.expirationDate);
    Campus = (TextView) findViewById(R.id.campusText);


    //declare the database reference object. This is what we use to access the database.
    mAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    myRef = mFirebaseDatabase.getReference();
    logout = (Button) findViewById(R.id.button_logout);
    user = mAuth.getCurrentUser();

    mAuthListener = new FirebaseAuth.AuthStateListener() {


        @Override
        public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
            user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                toastMessage("Successfully signed in with: " + user.getEmail());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
                toastMessage("Successfully signed out.");
            }

           //... I think a method declaration is meant to go here but not sure
        }
    };

    //new stuff
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            showData(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

private void showData(DataSnapshot dataSnapshot) {
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        UserInformation uInfo = new UserInformation();
        uInfo.setName(ds.child(userID).getValue(UserInformation.class).getName()); //set the name
        uInfo.setEmail(ds.child(userID).getValue(UserInformation.class).getEmail()); //set the email
        uInfo.setCampus(ds.child(userID).getValue(UserInformation.class).getCampus()); //set the phone_num
        uInfo.setExpiration_date(ds.child(userID).getValue(UserInformation.class).getExpiration_date());// set expiration date
        uInfo.setpfunds(ds.child(userID).getValue(UserInformation.class).getpfunds()); // get pantherfunds
        uInfo.setMeals(ds.child(userID).getValue(UserInformation.class).getMeals()); // get Meals
        uInfo.setTwop_num(ds.child(userID).getValue(UserInformation.class).getTwop_num()); // get Meals

       //display all the information
        Log.d(TAG, "showData: name: " + uInfo.getName());
        Log.d(TAG, "showData: email: " + uInfo.getEmail());
        Log.d(TAG, "showData: campus : " + uInfo.getCampus());
        Log.d(TAG, "showData: expiration_date: " + uInfo.getExpiration_date());
        Log.d(TAG, "showData: pfunds: " + uInfo.getpfunds());
        Log.d(TAG, "showData: : meals" + uInfo.getMeals());
        Log.d(TAG, "showData: twop_num: " + uInfo.getTwop_num());

        // Show Data in TextViews
        Email.append(uInfo.getEmail());
        TwoPNum.append(uInfo.getTwop_num());
        Meal.append(String.valueOf(uInfo.getMeals()));
        PantherFunds.append(String.valueOf(uInfo.getpfunds()));
        Expiration.append(String.valueOf(uInfo.getExpiration_date()));
        Campus.append(uInfo.getCampus());

        }
    }

I expect for a user to log in and their data be shown in text views but am getting this error message

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo            
{com.example.hootidapp/com.example.hootidapp.ProfileActivity}: 
java.lang.NullPointerException: Attempt to invoke virtual method         
'java.lang.String com.google.firebase.auth.FirebaseAuth.getUid()' on a 
null object reference

and

Caused by: java.lang.NullPointerException: Attempt to invoke virtual 
method 'java.lang.String com.google.firebase.auth.FirebaseAuth.getUid()' 
on a null object reference
at com.example.hootidapp.ProfileActivity.<init>(ProfileActivity.java:45)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
dHumphrey
  • 307
  • 2
  • 7
  • 24
  • It is not a duplicate! Because the duplicate is not showing me what to reference in order to pull it successfully! – dHumphrey May 23 '19 at 18:10

1 Answers1

2

In this line of code:

user = mAuth.getCurrentUser();

getCurrentUser() returned null because no user was signed in at the time it was called, and you're using auth before the auth listener was able to get a non-null user object. Check for null before calling getUid() on user. Or, ensure that you only call it after a user object is available.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441