-4

I have been having this issue a bunch when referencing my Firebase database. I think there is an issue with the data not actually being able to receive data from the database, or when it executes the first bit of code there, it doesn't actually have a user before it gets a UID. but I'm not entirely sure.

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.apex.quotable.managers.DatabaseHelper.getDatabaseReference()' on a null object reference at com.apex.quotable.managers.ProfileManager.isProfileExist(ProfileManager.java:61) at com.apex.quotable.activities.LoginActivity.checkIsProfileExist(LoginActivity.java:117) at com.apex.quotable.activities.LoginActivity.access$500(LoginActivity.java:30) at com.apex.quotable.activities.LoginActivity$4.onAuthStateChanged(LoginActivity.java:84) at com.google.firebase.auth.zzl.run(Unknown Source:24) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Then, it is saying the error comes from here:

checkIsProfileExist(user.getUid());

Then,

private void checkIsProfileExist(final String userId) {
    ProfileManager.getInstance(this).isProfileExist(userId, new OnObjectExistListener<Profile>() {
        @Override
        public void onDataChanged(boolean exist) {
            if (!exist) {
                startCreateProfileActivity();
            } else {
                PreferencesUtil.setProfileCreated(LoginActivity.this, true);
                DatabaseHelper.getInstance(LoginActivity.this.getApplicationContext())
                        .addRegistrationToken(FirebaseInstanceId.getInstance().getToken(), userId);
            }
            hideProgress();
            finish();
        }
    });
}

then

public void isProfileExist(String id, final OnObjectExistListener<Profile> onObjectExistListener) {
    DatabaseReference databaseReference = databaseHelper.getDatabaseReference().child("profiles").child(id);
    databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            onObjectExistListener.onDataChanged(dataSnapshot.exists());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
Amir Dora.
  • 2,831
  • 4
  • 40
  • 61
Jack Butler
  • 3
  • 1
  • 8

1 Answers1

0

Your databaseHelper is null. Make sure it has a value before calling methods on it. You're not showing enough code here to help us understand if or when you expect it to be assigned, but this is definitely the problem.

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