-3

here is my profile fragment where am having a java null I tried to add an online status feature in my profile fragment and I got this error Please help I need to finish this part today My profile fragment is down below also the log cat

here is my profile fragment where am having a java null I tried to add an online status feature in my profile fragment and I got this error Please help I need to finish this part today My profile fragment is down below also the log cat

//firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
private FirebaseMethods mFirebaseMethods;
private FirebaseUser firebaseUser;

//widgets
private TextView mPosts, mFollowers, mFollowing, mDisplayName, mUsername, mWebsite, mDescription;
private ProgressBar mProgressBar;
private CircleImageView mProfilePhoto;
private GridView gridView;
private Toolbar toolbar;
private ImageView profileMenu;
private BottomNavigationViewEx bottomNavigationView;
private Context mContext;


//vars
private int mFollowersCount = 0;
private int mFollowingCount = 0;
private int mPostsCount = 0;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_profile, container, false);
    mDisplayName = (TextView) view.findViewById(R.id.display_name);
    mUsername = (TextView) view.findViewById(R.id.username);
    mWebsite = (TextView) view.findViewById(R.id.website);
    mDescription = (TextView) view.findViewById(R.id.description);
    mProfilePhoto = (CircleImageView) view.findViewById(R.id.profile_photo);
    mPosts = (TextView) view.findViewById(R.id.tvPosts);
    mFollowers = (TextView) view.findViewById(R.id.tvFollowers);
    mFollowing = (TextView) view.findViewById(R.id.tvFollowing);
    mProgressBar = (ProgressBar) view.findViewById(R.id.profileProgressBar);
    gridView = (GridView) view.findViewById(R.id.gridView);
    toolbar = (Toolbar) view.findViewById(R.id.profileToolBar);
    profileMenu = (ImageView) view.findViewById(R.id.profileMenu);
    bottomNavigationView = (BottomNavigationViewEx) view.findViewById(R.id.bottomNavViewBar);
    mContext = getActivity();
    mFirebaseMethods = new FirebaseMethods(getActivity());
    Log.d(TAG, "onCreateView: stared.");


    setupBottomNavigationView();
    setupToolbar();

    setupFirebaseAuth();
    setupGridView();

    getFollowersCount();
    getFollowingCount();
    getPostsCount();

    TextView editProfile = (TextView) view.findViewById(R.id.textEditProfile);
    editProfile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: navigating to " + mContext.getString(R.string.edit_profile_fragment));
            Intent intent = new Intent(getActivity(), AccountSettingsActivity.class);
            intent.putExtra(getString(R.string.calling_activity), getString(R.string.profile_activity));
            startActivity(intent);
            getActivity().overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        }
    });

    return view;
}

private void status(String status){
    myRef = FirebaseDatabase.getInstance().getReference("users").child(firebaseUser.getUid());

    HashMap<String, Object> hashMap = new HashMap<>();
    hashMap.put("status", status);

    myRef.updateChildren(hashMap);
}

@Override
public void onResume() {
    super.onResume();
    status("online");
}

@Override
public void onPause() {
    super.onPause();
    status("offline");
}

Here is the log cat

     2020-10-15 13:30:36.012 16287-16287/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: umo.com.players, PID: 16287
java.lang.RuntimeException: Unable to resume activity {umo.com.players/umo.com.players.Profile.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3718)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3761)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2966)
    at android.app.ActivityThread.-wrap12(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6739)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
    at umo.com.players.Profile.ProfileFragment.status(ProfileFragment.java:140)
    at umo.com.players.Profile.ProfileFragment.onResume(ProfileFragment.java:151)
    at android.support.v4.app.Fragment.performResume(Fragment.java:2308)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1465)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
    at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3227)
    at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:3191)
    at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:214)
    at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:482)
    at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:471)
    at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:171)
    at android.app.Activity.performResume(Activity.java:7177)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3693)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3761) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2966) 
    at android.app.ActivityThread.-wrap12(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6739) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Kavea
  • 47
  • 5

1 Answers1

-1

Your error says FirebaseUser.getUid() on a null object reference and its on the line

myRef = FirebaseDatabase.getInstance().getReference("users").child(firebaseUser.getUid());

and as I see in your code you have not initialized firebaseUser variable, so you need to initialize firebaseUser before calling getUid() method on its instance, and you should be fine after that.

I got this referencece from this link

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // User is signed in
} else {
    // No user is signed in
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Aalap Patel
  • 2,058
  • 2
  • 17
  • 31