微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

'boolean kotlinx.coroutines.Deferred.start()' 在一个空对象引用上

如何解决'boolean kotlinx.coroutines.Deferred.start()' 在一个空对象引用上

当我并行执行大约 20-25 个异步任务时,我遇到了一个有趣的问题。

这是我截取代码

private val ioScope = Coroutinescope(dispatchers.IO)
private suspend fun getsquadPlayerDetails(
        repository: PlayersRepository,squadData: squadInclude
    ): List<TeamsquadMemberModel> {

        val playerTasks = mutablelistof<Deferred<GetPlayerByIdResponse?>>()
        val playerTasksResult = mutablelistof<GetPlayerByIdResponse?>()

        val playerDetailsJob = ioScope.launch {

            squadData.data.forEach { squadPlayer ->
         
                    val playerDetailsTask = async {
                        repository.getPlayerInfo(squadPlayer.player_id).response
                    }
                    playerTasks.add(playerDetailsTask)
                
            }

            val result = playerTasks.awaitAll()
            playerTasksResult.addAll(result)
        }

        playerDetailsJob.join()
}

2021-02-08 09:03:33.878 13338-15116/E/AndroidRuntime:致命异常:Defaultdispatcher-worker-3 进程:,PID:13338 java.lang.NullPointerException:尝试在空对象引用上调用接口方法“boolean kotlinx.coroutines.Deferred.start()” 在 kotlinx.coroutines.AwaitAll.await(Await.kt:66) 在 kotlinx.coroutines.AwaitKt.awaitAll(Await.kt:38) 在 $1.invokeSuspend(GetTeamDetailsUseCase.kt:70) 在 kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 在 kotlinx.coroutines.dispatchedTask.run(dispatchedTask.kt:56) 在 kotlinx.coroutines.scheduling.Coroutinescheduler.runSafely(Coroutinescheduler.kt:571) 在 kotlinx.coroutines.scheduling.Coroutinescheduler$Worker.executeTask(Coroutinescheduler.kt:738) 在 kotlinx.coroutines.scheduling.Coroutinescheduler$Worker.runWorker(Coroutinescheduler.kt:678) 在 kotlinx.coroutines.scheduling.Coroutinescheduler$Worker.run(Coroutinescheduler.kt:665)

看起来有时由于某种原因正在执行的协程抛出异常但为什么我无法弄清楚。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。