Crash for desktop but works fine for Android and emulator

I have some code that works great for Android and the emulator, but when I try to run it from the desktop it crashes with a null pointer exception. Can anyone see what it is I am doing wrong?

object RealmRepo {
    lateinit var realm: Realm
    var userEmail = ""
    var practiceName = ""
    private var initialized = false
    private lateinit var registerPractice : Practice
    private lateinit var userAccount : UserAccount

    private val appServiceInstance by lazy {
        // If logs are on app level then it set for everything ..
        val configuration =
            AppConfiguration.Builder("application-0-rpbsh")
                .log(LogLevel.ALL).build()

        App.create(configuration)        //***        Null pointer happens here     ***

    }

    init {
        if (!initialized) {
            setupRealmSync()
            initialized = true
        } else {
            throw IllegalStateException("Singleton instance already created.")
        }
    }
    private fun setupRealmSync() {
        val user = runBlocking { appServiceInstance.currentUser!! }
        val config = SyncConfiguration
            .Builder(
                user,
                setOf(UserAccount::class,Pellet::class, PelletInsertionVisit::class, PelletLot::class, Patient::class,
                    Practice::class )
            )
            .initialSubscriptions(rerunOnOpen = true) { realms ->
                add(
                    realms.query<PelletInsertionVisit>(),
                    name = "patient info",
                    updateExisting = true
                )
                add(realms.query<PelletLot>(), name = "Pellet info", updateExisting = true)
                add(realms.query<Patient>(), name = "Patient info", updateExisting = true)
                add(realms.query<Practice>(), name = "Practice info", updateExisting = true)
                add(realms.query<UserAccount>(), name = "UserAccount info", updateExisting = true)

            }
            .build()
        realm = Realm.open(config)
    }

Hi @Darrin_Smith1,

To better understand the issue, please share the following:

  1. Error message with stacktrace.
  2. Code where the RealmRepo object and function are used.

I would also like to clarify what you mean by “run it from the desktop”. And how are you verifying that it is working properly on Android/Emulator?

Are you also referring to this doc for setting up Realm Device Sync?

1 Like

It was a null pointer.

class HomeScreen : Screen {
    private var userNameSetSignIn = ""
    private var passwordSetSignIn = ""
    private var userNameSetSignUp = ""
    private var passwordSetSignUp = ""
    private var practiceSetSignUp = ""
    private var practiceSetSignIn = ""
    private var signUp = "Sign Up"
    private var signIn= "Sign In"

    @OptIn(ExperimentalResourceApi::class)
    @Preview
    @Composable
    override fun Content() {
        val navigator = LocalNavigator.currentOrThrow
        val showSignInNeededDialog = remember { mutableStateOf(false) }
        val showSignUpNeededDialog = remember { mutableStateOf(false) }
        val scrollState = rememberScrollState()
        val realm = RealmRepo

Hi @Darrin_Smith1,

Can you share the full error stack trace?

At first glance, it seems like an initialization issue with the RealmRepo in that class. Some references to init issues:

  1. android - Kotlin data object becomes null - Stack Overflow
  2. android - Properly initialize heavy objects - Stack Overflow

It can be fixed, temporarily, by deleting the mongodb-realm directory that gets created for each run in Android Studio.