0

I have created an android chat app using firebase - the app works fine but when first opened, the app crashes in login page but after this the app restarts and works fine. Please help me with a solution because I am a beginner in this field tell me what I should change to make it work

My logcat: beginning of crash

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 com.dndna.chat_rooms.views.base.BasePresenter.getCurrentUserId(BasePresenter.java:78) at com.dndna.chat_rooms.views.main.MainPresenter.checkPendingNotification(MainPresenter.java:198) at com.dndna.chat_rooms.views.main.MainActivity.onResume(MainActivity.java:262) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1456) at android.app.Activity.performResume(Activity.java:7614) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4412)

The problem seems to be in these locations

public void checkPendingNotification() {
    if (getCurrentUserId() != null)
        notifyManager.checkPendingNotification(this::showBadgeAt);
}

and

if (presenter != null) {
    presenter.checkPendingNotification();
}

and

protected String getCurrentUserId() {
    FirebaseAuth.getInstance();
    return FirebaseAuth.getInstance().getCurrentUser().getUid();
}
AurumTechie
  • 166
  • 3
  • 14
  • You're calling `FirebaseAuth.getInstance().getCurrentUser().getUid();` when `FirebaseAuth.getInstance().getCurrentUser()` returns `null`. The solution is to detect that `FirebaseAuth.getInstance().getCurrentUser()` is null, and then do something else in your code (since the user isn't signed in yet). – Frank van Puffelen May 25 '20 at 04:18
  • thanks for your comment but can you please explain what shold i do and code snippet ? – Ahmed Yasser May 25 '20 at 04:28
  • I think your google-serives.json has some invalid keys which is telling you unauthrized... Please get latest google-services.json file Basicaly firebase config file – Rohit May 25 '20 at 04:40
  • @FrankvanPuffelen explained what you should do. Check that getCurrentUser() for null like you did in your other code segments. – NomadMaker May 25 '20 at 04:50
  • @Rohit did not solve any thing yet still crashing at first open – Ahmed Yasser May 25 '20 at 05:09
  • wen i tryed to change from if (presenter != null) { to if (presenter == null) { its work fine in android api 23 but crashes in all other apis – Ahmed Yasser May 25 '20 at 05:22
  • @FrankvanPuffelen Fair enough, puf. I just deleted the comment. Thanks. – Alex Mamo May 25 '20 at 14:38

0 Answers0