Firebase Auth getCurrentUser.getUid NullPointerException

时间:2018-08-28 10:01:19

标签: java android firebase firebase-authentication nullreferenceexception

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.mfymn.chatin, PID: 24476
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mfymn.chatin/com.mfymn.chatin.ui.MainActivity}: 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.performLaunchActivity(ActivityThread.java:2779)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2844)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
                  at android.os.Handler.dispatchMessage(Handler.java:110)
                  at android.os.Looper.loop(Looper.java:203)
                  at android.app.ActivityThread.main(ActivityThread.java:6364)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
               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.mfymn.chatin.ui.fragment.RequestsFragment.onCreateView(RequestsFragment.java:84)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428)
                  at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
                  at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
                  at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
                  at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
                  at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
                  at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)
                  at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)
                  at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
                  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
                  at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
                  at com.mfymn.chatin.ui.MainActivity.onStart(MainActivity.java:85)
                  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
                  at android.app.Activity.performStart(Activity.java:6683)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2742)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2844) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572) 
                  at android.os.Handler.dispatchMessage(Handler.java:110) 
                  at android.os.Looper.loop(Looper.java:203) 
                  at android.app.ActivityThread.main(ActivityThread.java:6364) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 

启动应用程序并崩溃时出现此错误。我想从请求片段中检索uid时会出现问题。你能帮助我吗?下面提到了Java文件。

MainActivity.java

RequestsFragment.java

3 个答案:

答案 0 :(得分:0)

应该空检查FirebaseUser(),因为当用户未登录时,它可以为空。

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    if (user != null && !TextUtils.isEmpty(user.getUid())) {

        mUserRef = FirebaseDatabase.getInstance().getReference().child("Users").child(user.getUid());

    }

答案 1 :(得分:0)

RequestFragment.java 中尝试此操作:

if((online_user_id = FirebaseAuth.getInstance().getCurrentUser().getUid())!=null){
    FriendsRequestReference = FirebaseDatabase.getInstance().getReference().child("Friend_req").child(online_user_id);
    UsersReference = FirebaseDatabase.getInstance().getReference().child("Users");
    FriendsDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Friends");
    FriendsReqDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Friend_req");
};

第88行

答案 2 :(得分:0)

要解决此问题,您应该在实际使用 test.df<-as.data.frame(c(1:50)) if (test.df[,c(27:50)] <30){ print("hi")} 对象之前首先检查是否为空,如下所示:

FirebaseUser