How can i fix this? “Method invocation 'isEmailVerified' may produce 'java.lang.NullPointerException'” [duplicate]

不问归期 提交于 2020-01-06 07:43:04

问题


I tried to solve it but I can not do anything, I always get the NPE even with the try and catch, I do not know what else to do, please some help,

what is the correct way to remove the npe? What should I put?

try{
    if(user.isEmailVerified()){
      Log.d(TAG, "onComplete: success. email is verified.");
      Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
      startActivity(intent);
    } else {
      Toast.makeText(mContext, "Email is not verified \n check your email inbox.", Toast.LENGTH_SHORT).show();
      mProgressbar.setVisibility(View.GONE);
      mPleaseWait.setVisibility(View.GONE);
      mAuth.signOut();
    }
} catch (NullPointerException e){
    Log.e(TAG, "onComplete: NullPointerException: " + e.getMessage() );
}

when I delete the progress bar the program is executed but then I get all this in the logcat...

02-18 21:03:17.670 1492-1492/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.togo.plgl.togo, PID: 1492
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.togo.plgl.togo/com.togo.plgl.togo.Home.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx.enableAnimation(boolean)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:156)
        at android.app.ActivityThread.main(ActivityThread.java:6523)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx.enableAnimation(boolean)' on a null object reference
        at com.togo.plgl.togo.utils.BottomNavigationViewHelper.setupBottomNavigationView(BottomNavigationViewHelper.java:26)
        at com.togo.plgl.togo.Home.HomeActivity.setupBottomNavigationView(HomeActivity.java:95)
        at com.togo.plgl.togo.Home.HomeActivity.onCreate(HomeActivity.java:51)
        at android.app.Activity.performCreate(Activity.java:6915)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:156) 
        at android.app.ActivityThread.main(ActivityThread.java:6523) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832) 
02-18 21:03:17.683 1492-1492/? I/Process: Sending signal. PID: 1492 SIG: 9 

回答1:


Try-catch in this situation will prevent your app from possible crash. That is just a Lint warning.

If you want to remove the warning. First check if the variable user is null.

if (user == null){
    // show error
    return;
}
// your code



回答2:


Before calling isEmailverified check for user. User may be null, so when you try to call a method on null compiler will through null pointer exception.

Try if( user != Null and user.isEmailVerified)




回答3:


If you are using mAuth to log your users you can try

if(mAuth.getCurrentUser() != null) {

//do your stuff
if(user.isEmailVerified()){ ...}

}

or

if(((mAuth.getCurrentUser() != null)&&(user.isEmailVerified()))) {

//do your stuff
   Log.d(TAG, "onComplete: success. email is verified."); ....

}

before you ask for the method isEmailVerified();

so you can guarantee that you have an user connected before requesting information from them



来源:https://stackoverflow.com/questions/48858721/how-can-i-fix-this-method-invocation-isemailverified-may-produce-java-lang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!