0

I have Google Map Activity that launches after a user logs in using Back4App Parse server. In the onCreate method of this activity, I can print the username to LogCat with a call to ParseUser.getCurrentUser().getUsername(). I can print it again in the onMapReady method with the same call. However, when I go to log the user out after clicking the logout button, ParseUser.getCurrentUser().getUsername() returns null and crashes my app. I know I can store the username in a String when the call does work, but I want to know WHY the user is inaccessible in my onClick method.

public class PassengerMapActivity extends FragmentActivity implements OnMapReadyCallback,
                                                                      View.OnClickListener {
  private GoogleMap map;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_passenger_map);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    Log.i("onCreate", ParseUser.getCurrentUser().getUsername()); // this works 
  }

  @Override
  public void onMapReady(GoogleMap googleMap) {
    map = googleMap;

    Log.i("onMapReady", ParseUser.getCurrentUser().getUsername()); // this works too

    locationListener = new LocationListener() {
      @Override
      public void onLocationChanged(Location location) {
        updateMapCamera(location);
      }
      ... // unused interface methods
    };
  }

  @Override
  public void onClick(View view) {
    switch(view.getId()) {
      case R.id.btn_logout_passenger:
        final String currentUser = ParseUser.getCurrentUser().getUsername(); // this returns null
        ParseUser.logOutInBackground(new LogOutCallback() {
          @Override
          public void done(ParseException e) {
            if(e == null) {
              Toast.makeText(PassengerMapActivity.this,  
              currentUser + " has logged out", Toast.LENGTH_SHORT).show();
            }
          }
        });
        break;

Log Info: 2020-03-22 17:11:47.035 2969-2969/packageName E/AndroidRuntime: FATAL EXCEPTION: main Process: packageName, PID: 2969 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.parse.ParseUser.getUsername()' on a null object reference

gig6
  • 307
  • 5
  • 16
  • I was able to work around this by instantiating a ParseUser member variable and assigning it in the onCreate method where the ParseUser.getCurrentUser() is accessible. However, I would still like to know the answer to my original question. – gig6 Mar 21 '20 at 02:29
  • Maybe your onClick method is firing twice? Can you please add some log right after getting `currentUser`? – Davi Macêdo Mar 21 '20 at 18:49
  • @DaviMacêdo I have included the relevant log error below the code. The ParseUser object is null. – gig6 Mar 23 '20 at 00:16
  • It is probably being logged out somewhere else in your app or this function is being fired twice. If you remove the `ParseUser.logOutInBackground(new LogOutCallback() {` part, do you still have the same problem? – Davi Macêdo Mar 23 '20 at 00:22
  • @DaviMacêdo There are no other calls to logout in the code. Even so, the call to ParseUser.getCurrentUser() gets called before logout method does. But I did delete the code as you asked and yes, it still has the same error. – gig6 Mar 23 '20 at 01:29
  • 1
    It looks that your problem is not related to this specific part of your code but you have something else making your current user to loose the session. I'd try to debug the execution line by line and check in which line the current user is being lost. – Davi Macêdo Mar 23 '20 at 20:58

0 Answers0