Unable to query documents in Atlas cluster

I am trying to query documents in an Atlas cluster, specifically from my AppUser collection. The intended functionality is that a user logs in and then their respective AppUser would be queried; I would be able to log in to any account on any device.

However, I was encountering many issues. See the code:

Credentials credentials = Credentials.emailPassword(email, password);
app.loginAsync(credentials, result -> {
   SyncConfiguration config = new SyncConfiguration.Builder(app.currentUser(), PARTITION)
	.waitForInitialRemoteData().build();
   realm = Realm.getInstance(config);

   RealmResults<AppUser> users = realm.where(AppUser.class).findAll();
   AppUser user = null;

   // Find the AppUser with the email
   for (int i = 0; i < users.size(); i++) {
      if (users.get(i).getEmail().equals(email)) {
         user = users.get(i);
      }
   }
   if (user == null) Log.wtf("EXAMPLE", "This should NEVER happen."); // but it happens anyway, causing the exception below
   String type = user.getUserType(); // *** EXCEPTION here ***
   // ...
});

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jchan.testing, PID: 13568
java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String com.jchan.testing.AppUser.getUserType()’ on a null object reference
at com.jchan.testing.MainActivity.lambda$onCreate$0$com-jchan-testing-MainActivity(MainActivity.java:120)
at com.jchan.testing.MainActivity$$ExternalSyntheticLambda2.onResult(Unknown Source:6)
at io.realm.internal.mongodb.Request$3.run(Request.java:90)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Supposedly, the ‘jchan’ AppUser should have been queried after entering the credentials for the ‘jchan’ User. The code above does work; locally that is. However, after wiping my emulator, reinstalling, and attempting to log in with the same credentials, I get the above exception. I did not have problems registering a User and AppUser into the database using a synced realm though. That brings me to one question:

Why isn’t the synced realm getting documents from my remote Atlas cluster?

Maybe it can only upload documents and can’t download them, but I’m certain this isn’t the case. I have tried:

  • Terminating and reinitializing sync (did this with the other below fixes)
  • Wiping the emulator and reinstalling (did this with the other below fixes)
  • Deleting all schema configurations and the databases in the cluster (would then use development mode to get them back)
  • Invalidating caches in Android Studio
  • Looking for similar issues other people had (a few hours of browsing, none of them helped)

Next, I tried to query the cluster directly by referring to this documentation. But of course, it still doesn’t work:

Credentials credentials = Credentials.emailPassword(email, password);
app.loginAsync(credentials, result -> {
   MongoClient mongoClient = app.currentUser().getMongoClient("mongodb-atlas");
   MongoDatabase mongoDatabase = mongoClient.getDatabase("MyDB");
   CodecRegistry pojoCodecRegistry = fromRegistries(AppConfiguration.DEFAULT_BSON_CODEC_REGISTRY,
      fromProviders(PojoCodecProvider.builder().automatic(true).build()));
   MongoCollection<AppUser> mongoCollection = mongoDatabase.getCollection("AppUser", AppUser.class)
      .withCodecRegistry(pojoCodecRegistry);
   Document queryFilter = new Document("email", email);
   AppUser user = mongoCollection.findOne(queryFilter).get(); // *** EXCEPTION here ***
   String type = user.getUserType();
   // ...
});

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jchan.testing, PID: 13389
NETWORK_UNKNOWN(realm::app::CustomError:1002)
android.os.NetworkOnMainThreadException
at io.realm.internal.network.NetworkRequest.resultOrThrow(NetworkRequest.java:84)
at io.realm.internal.objectstore.OsMongoCollection.findOneInternal(OsMongoCollection.java:274)
at io.realm.internal.objectstore.OsMongoCollection.findOne(OsMongoCollection.java:224)
at io.realm.mongodb.mongo.MongoCollection$6.run(MongoCollection.java:236)
at io.realm.internal.async.RealmResultTaskImpl.get(RealmResultTaskImpl.java:92)
at com.jchan.testing.MainActivity.lambda$onCreate$0$com-jchan-testing-MainActivity(MainActivity.java:126)
at com.jchan.testing.MainActivity$$ExternalSyntheticLambda2.onResult(Unknown Source:6)
at io.realm.internal.mongodb.Request$3.run(Request.java:90)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

    at io.realm.internal.network.NetworkRequest.resultOrThrow(NetworkRequest.java:84)
    at io.realm.internal.objectstore.OsMongoCollection.findOneInternal(OsMongoCollection.java:274)
    at io.realm.internal.objectstore.OsMongoCollection.findOne(OsMongoCollection.java:224)
    at io.realm.mongodb.mongo.MongoCollection$6.run(MongoCollection.java:236)
    at io.realm.internal.async.RealmResultTaskImpl.get(RealmResultTaskImpl.java:92)
    at com.jchan.testing.MainActivity.lambda$onCreate$0$com-jchan-testing-MainActivity(MainActivity.java:126)
    at com.jchan.testing.MainActivity$$ExternalSyntheticLambda2.onResult(Unknown Source:6)
    at io.realm.internal.mongodb.Request$3.run(Request.java:90)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

I’m not sure why this didn’t work. If anyone can help me with this, I would give my utmost appreciation.

I know its been a year for this post, but this answer might help someone. The first Error showing the user is null. Therefore, make sure that your not getting a null User Credentials. Second error is because your using get() instead of getAsync() then you can iterate the values.

Credentials was definitely not null back when I tested this. Email and password were proper Strings and the matching user was also in the database. As for get() instead of getAsync(), that might be a valid solution but the error states that the exception occurred on the main thread, even though it was under loginAsync(). Plus, it would seem unnecessary to use multiple threads for sequential tasks.