0

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 
daniu
  • 14,137
  • 4
  • 32
  • 53
  • Are you sure that this is the only case in which you're using `isEmailVerified()`? Also consider testing for `null` instead of using exceptions. – synchronizer Feb 19 '18 at 02:18
  • I delete the progressbar and it run, so I suposse I have to delete that – Pedro Lizarraga Feb 19 '18 at 02:19
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Feb 19 '18 at 02:59
  • .. 4) Change `Log.e(TAG, "onComplete: NullPointerException: " + e.getMessage() );` to `e.printStackTrace(); Log.e(TAG, "onComplete: NullPointerException: " + e.getMessage() );` – Andrew Thompson Feb 19 '18 at 03:00
  • when I delete the progressbar it works – Pedro Lizarraga Feb 19 '18 at 03:19

3 Answers3

0

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
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

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)

0

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

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77