-1

Help with this code please am getting errors and my app is crashing.

Code:

 super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_restoration_connect2);
    mAuth = FirebaseAuth.getInstance();
    currentUser = mAuth.getCurrentUser();
    currentUserID = mAuth.getCurrentUser().getUid();
    RootRef = FirebaseDatabase.getInstance().getReference();
    RootRef.keepSynced ( true );

Exception:

2019-10-25 16:17:14.656 23240-23240/com.example.ezvisa
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ezvisa, PID: 23240
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ezvisa/com.example.ezvisa.RestorationConnect}:
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:6368)
        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.example.ezvisa.RestorationConnect.onCreate(RestorationConnect.java:51)
        at android.app.Activity.performCreate(Activity.java:6666)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2732)
        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:6368) 
        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)
zappee
  • 20,148
  • 14
  • 73
  • 129
  • My guess is FirebaseAuth.getInstance() is returning a null object. Either null check this object or figure out why that returns null – Robb Oct 25 '19 at 13:26
  • This might be a nit: you're getting the current user and saving it in a variable then _not_ using that var in the next line. It'll probably help to check for a null `currentUser` and check the docs for _when_ the return from `getCurrentUser()` may be null and work from there. – Hyperbole Oct 25 '19 at 13:27
  • Well it seems that `mAuth.getCurrentUser()` is null. – Maurice Perry Oct 25 '19 at 13:36
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Progman Oct 25 '19 at 18:51

2 Answers2

0

It is giving a null pointer exception when trying to get the uid. It is posssible that no user is signed in so put it in a condition block like:

FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
    String currentUserID = user.getUid();
}

The firebase website has also very good documentation in case you want to follow through: https://firebase.google.com/docs/auth/android/start

0

You have to decide whether or not you want to allow the creation of this activity while mAuth.getCurrentUser() is null. You can either prevent the creation or allow it, but in the latter case, you must make sure that your code can deal with the nulls.

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24