Kotlin coroutine delay() function causes java.lang.NoSuchMethodError

1.6k Views Asked by At

I have a simple Kotlin app that calls a web service every 10 seconds via Ktor, and uses coroutines to do this. In the coroutine, when I use the delay() function to pause for 10 seconds, I get the below error. When I remove the delay() function, the code works fine. Any ideas on why using the delay() function is causing this error?

The code:

@OptIn(ExperimentalTime::class)
class NOAAUpdater(private val timerScope: CoroutineScope) {

    @OptIn(InternalCoroutinesApi::class)
    fun start(): Job {
        return timerScope.launch {
            while (isActive) {
                val client = HttpClient(CIO)
                val response: String = client.get(url)
                client.close()
                logger.debug { response }
                delay(10000)
            }
        }
    }
}
@OptIn(ExperimentalTime::class)
suspend fun main() {

    logger.info { "App Started" }
    val noaaUpdater = NOAAUpdater(CoroutineScope(Dispatchers.Default))
    noaaUpdater.start()

    

}

The error:

Exception in thread "DefaultDispatcher-worker-1" java.lang.NoSuchMethodError: kotlinx.coroutines.DelayKt.delay-p9JZ4hM(JLkotlin/coroutines/Continuation;)Ljava/lang/Object;
    at com.me.starter.NOAAUpdater$start$2.invokeSuspend(NOAAUpdater.kt:41)
    at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:191)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:147)
    at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(SuspendFunctionGun.kt:15)
    at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(SuspendFunctionGun.kt:93)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
    at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:191)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:147)
    at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(SuspendFunctionGun.kt:15)
    at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(SuspendFunctionGun.kt:93)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
0

There are 0 best solutions below