Search code examples
javaobjectnullpointerexceptionrealm-mobile-platform

in Realm, Why is SyncUser.logInAsync() not entering onSuccess() or onError()?


I am currently trying to login to create a login to realm, but I am unable to access a SyncUser because my logInAsync is not entering the onSuccess or onError method. The onSuccess method returns a SyncUser, but this is not occurring. My code is

    String authURL = "https://my-realm.us1a.cloud.realm.io";
    String email = "[email protected]";
    String password = "testPassword";
    SyncCredentials credentials = SyncCredentials.usernamePassword(email, password, true);
    RealmAsyncTask task = SyncUser.logInAsync(credentials, authURL, new SyncUser.Callback<SyncUser>() {
        @Override
        public void onSuccess(SyncUser result) {
            user = result;
        }

        @Override
        public void onError(ObjectServerError error) {
            System.out.println("failed");
        }
    });
    String url = "realms://unbranded-metal-bacon.us1a.cloud.realm.io/~/travelin";
    config = user.createConfiguration(url).build();

I have a private SyncUser user declared above. In the last line, I am getting a null object reference because user is null. What is the reason that I am not entering onSuccess or onError?


Solution

  • Auth URL is wrong, need to be

    String authURL = "https://my-realm.us1a.cloud.realm.io/auth";
    

    Also, SyncUser.logInAsync() runs in background so this line need to be inside onSuccess(), otherwise User will be null

    config = user.createConfiguration(url).build();