-1

“I’m setting a map activity where i get updated location in real time database fire base but not show location in fire base .when i logged in my app then driver map activity open and app crashes. I see log cat error show null pointer exception.

error on first line getUid() 

( 168 line  no)    
private void DisconnectTheDriver() {

    String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference DriverAvailibiltyRef = FirebaseDatabase.getInstance().getReference().child("Drivers Available");

    GeoFire geoFire = new GeoFire(DriverAvailibiltyRef);

(175 line no)        geoFire.removeLocation(userID);


}

In logcat

 at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6942)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 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.uolbuses.DriverMapActivity.DisconnectTheDriver(DriverMapActivity.java:275)
    at com.example.uolbuses.DriverMapActivity.onStop(DriverMapActivity.java:268)
    at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1382)
    at android.app.Activity.performStop(Activity.java:7452)
    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4238)
    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4290) 
    at android.app.ActivityThread.-wrap25(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1730) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6942) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

1

One possible reason could be that you are accessing a page where the userID is not defined. If you have login feature in your app, then I would recommend going down that path and then reaching this.

Once you reach to this page, use your debugger and see the value for uid.

Again I am assuming a lot of stuff here, if you could provide some more insight about the workflow, that would be helpful

A little code snippet incase you are missing this

import com.google.firebase.auth.FirebaseAuth;
...
...

if (FirebaseAuth.getInstance().getCurrentUser() == null) {
    //Go to login 
}
else{
    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
}
static const
  • 953
  • 4
  • 16