https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

Jan

08/04/2022, 5:32 PM
What could cause this exception? (thread)
Copy code
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "kotlin.coroutines.CoroutineContext.fold(Object, kotlin.jvm.functions.Function2)" because "$this$hasCopyableElements" is null
	at kotlinx.coroutines.CoroutineContextKt.hasCopyableElements(CoroutineContext.kt:40)
	at kotlinx.coroutines.CoroutineContextKt.foldCopies(CoroutineContext.kt:54)
	at kotlinx.coroutines.CoroutineContextKt.newCoroutineContext(CoroutineContext.kt:19)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:52)
	at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
	at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
	at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
	at io.github.jan.supacompose.auth.AuthImpl.<init>(AuthImpl.kt:54)
	at io.github.jan.supacompose.auth.Auth$Companion.create(Auth.kt:149)
	at io.github.jan.supacompose.auth.Auth$Companion.create(Auth.kt:143)
	at io.github.jan.supacompose.SupabaseClientBuilder.install$lambda-1(SupabaseClientBuilder.kt:34)
	at io.github.jan.supacompose.SupabaseClientImpl.<init>(SupabaseClient.kt:44)
	at io.github.jan.supacompose.SupabaseClientBuilder.build(SupabaseClientBuilder.kt:23)
	at MainKt.main(Main.kt:132)
	at MainKt$main$3.invoke(Main.kt)
	at MainKt$main$3.invoke(Main.kt)
	at kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt$createCoroutineUnintercepted$$inlined$createCoroutineFromSuspendFunction$IntrinsicsKt__IntrinsicsJvmKt$1.invokeSuspend(IntrinsicsJvm.kt:205)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlin.coroutines.ContinuationKt.startCoroutine(Continuation.kt:115)
	at kotlin.coroutines.jvm.internal.RunSuspendKt.runSuspend(RunSuspend.kt:19)
	at MainKt.main(Main.kt)
AuthImpl:
Copy code
supabaseClient.launch {
    Napier.d {
        "Trying to load latest session"
    }
    _status.value = Auth.Status.LOADING_FROM_STORAGE
    val session = sessionManager.loadSession(supabaseClient, this@AuthImpl)
    if (session != null) {
        Napier.d {
            "Successfully loaded session from storage"
        }
        startJob(session)
    } else {
        _status.value = Auth.Status.NOT_AUTHENTICATED
    }
}
n

Nick Allen

08/04/2022, 6:01 PM
Kinda looks like your CoroutineScope's coroutineContext property is returning null
j

Jan

08/04/2022, 6:04 PM
oohh yes thats kinda true the AuthImpl constructor was called before the coroutineContext property was intialized
thanks
n

Nick Allen

08/04/2022, 6:06 PM
FYI, I'd avoid implementing CoroutineScope yourself and instead rely on the builders available.
534 Views