Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when passing exceptionHandler in viewModelScope.launch #2080

Open
daniel-waiguru opened this issue Dec 16, 2024 · 4 comments
Open

Crash when passing exceptionHandler in viewModelScope.launch #2080

daniel-waiguru opened this issue Dec 16, 2024 · 4 comments
Labels
question Usage question

Comments

@daniel-waiguru
Copy link

daniel-waiguru commented Dec 16, 2024

Describe the bug
In my ViewModel i use need to specify an exception handler when launching a coroutine in viewModelScope as below

kotlin private val exceptionHandler = CoroutineExceptionHandler { _, throwable -> } viewModelScope.launch(exceptionHandler) { //code }

but I noticed that the ViewModel injection fails and causes a crash with stacktrace as below, however upon removing the exceptionHandler the app runs without a crash
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:590) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)  Caused by: org.koin.core.error.InstanceCreationException: Could not create instance for '[Factory: 'profile_presentation.EditProfileViewModel']' at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:58) at org.koin.core.instance.FactoryInstanceFactory.get(FactoryInstanceFactory.kt:38) at org.koin.core.registry.InstanceRegistry.resolveInstance$koin_core(InstanceRegistry.kt:110) at org.koin.core.scope.Scope.resolveFromRegistry(Scope.kt:321) at org.koin.core.scope.Scope.resolveFromContext(Scope.kt:311) at org.koin.core.scope.Scope.stackParametersCall(Scope.kt:281) at org.koin.core.scope.Scope.resolveInstance(Scope.kt:259) at org.koin.core.scope.Scope.resolveWithOptionalLogging(Scope.kt:232) at org.koin.core.scope.Scope.get(Scope.kt:215) at org.koin.viewmodel.factory.KoinViewModelFactory.create(KoinViewModelFactory.kt:39) at androidx.lifecycle.viewmodel.ViewModelProviderImpl_androidKt.createViewModel(ViewModelProviderImpl.android.kt:34) at androidx.lifecycle.viewmodel.ViewModelProviderImpl.getViewModel$lifecycle_viewmodel_release(ViewModelProviderImpl.kt:65) at androidx.lifecycle.viewmodel.ViewModelProviderImpl.getViewModel$lifecycle_viewmodel_release$default(ViewModelProviderImpl.kt:47) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.android.kt:91) at org.koin.viewmodel.GetViewModelKt.resolveViewModel(GetViewModel.kt:60) at org.koin.viewmodel.GetViewModelKt.resolveViewModel$default(GetViewModel.kt:44 at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3152) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:608) at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:286) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2214) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2115) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2052) at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:703) at android.os.Handler.handleCallback(Handler.java:959) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loopOnce(Looper.java:232) at android.os.Looper.loop(Looper.java:317) at android.app.ActivityThread.main(ActivityThread.java:8705) at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)  Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object kotlin.coroutines.CoroutineContext.fold(java.lang.Object, kotlin.jvm.functions.Function2)' on a null object reference at kotlinx.coroutines.CoroutineContextKt.hasCopyableElements(CoroutineContext.kt:36) at kotlinx.coroutines.CoroutineContextKt.foldCopies(CoroutineContext.kt:51) 2024-12-16 19:25:17.004 13141-13141 AndroidRuntime E at kotlinx.coroutines.CoroutineContextKt.newCoroutineContext(CoroutineContext.kt:15) at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:48) at kotlinx.coroutines.BuildersKt.launch(Unknown Source:1) at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:43) at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source:1) at at presentation.di.FeatureProfileModulesKt.viewModelModules$lambda$1$lambda$0(FeatureProfileModules.kt:9) at presentation.di.FeatureProfileModulesKt.$r8$lambda$I4C-DS2Jr0O4j89h8zyZwNHL4z8(Unknown Source:0) at presentation.di.FeatureProfileModulesKt$$ExternalSyntheticLambda2.invoke(D8$$SyntheticClass:0) at org.koin.core.instance.InstanceFactory.create(InstanceFactory.kt:51)

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
The app should not crash and koin should be able to create an instance of the viewmodel

Koin module and version:
[e.g]: koin-android:4.0.0

Snippet or Sample project to help reproduce
Add a snippet or even a small sample project to help reproduce your case.

@arnaudgiuliani arnaudgiuliani added the question Usage question label Jan 10, 2025
@arnaudgiuliani
Copy link
Member

aren't you mixing Koin scope with Coroutines Scope?

@daniel-waiguru
Copy link
Author

not really, when i have viewModelScope.launch {} koin is able to create and inject the VM but I add exceptionhandler koin fails to create the VM

@arnaudgiuliani
Copy link
Member

there is a weird call on your EditProfileViewModel can you try to track why here?

@daniel-waiguru
Copy link
Author

the only difference is I introduced coroutineexceptionhandler when launching a coroutine in the ViewModel scope

private val exceptionHandler = CoroutineExceptionHandler { _, throwable -> } viewModelScope.launch(exceptionHandler) { //code }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Usage question
Projects
None yet
Development

No branches or pull requests

2 participants