bj0
12/26/2017, 5:41 PMlaunch {}
in a button click handler: java.lang.NullPointerException: Attempt to invoke interface method 'kotlin.coroutines.experimental.CoroutineContext$Element kotlin.coroutines.experimental.CoroutineContext.get(kotlin.coroutines.experimental.CoroutineContext$Key)' on a null object reference
dave08
12/26/2017, 7:04 PMbj0
12/26/2017, 7:45 PMdave08
12/26/2017, 8:35 PMbj0
12/26/2017, 8:46 PMview.data_button.setOnClickListener {
if (datCon.isStarted)
datCon.stop()
else {
warn { "launching" }
launch {
warn { "launch" }
delay(3000)
// try {
// datCon.start()
// } catch (e: Exception) {
// err { ":${e.message}" }
// }
}
}
}
onCreateView
from a Fragment
dave08
12/26/2017, 9:18 PMlaunch
tries retreiving a Job
from the CoroutineContext
that you pass to the launch(context)
, it looks like that context is null sometimes.... maybe try a breakpoint or log there to check its value... and when the coroutine actually gets started.launch
since it runs by default on CommonPool
context and not UI
launch
will still be running, and a new one will get created at every click.. you might get a leak there...bj0
12/26/2017, 9:29 PMCommonPool
, and I'm not touching any UI elements. I'm not worried about leaks because this fragment is embedded and doesn't get cleaned up until the app is closeddave08
12/26/2017, 9:37 PMlaunch
with an async { }.await()
there might be a hiddwn exception. Or if it's a race, maybe try a conflated actor like in the ui guide:
fun Node.onClick(action: suspend (MouseEvent) -> Unit) {
// launch one actor to handle all events on this node
val eventActor = actor<MouseEvent>(UI, capacity = Channel.CONFLATED) { // <--- Changed here
for (event in channel) action(event) // pass event to action
}
// install a listener to offer events to this actor
onMouseClicked = EventHandler { event ->
eventActor.offer(event)
}
}
val parentJob = Job()
in the Fragment, and running launch(CommonPool + parentJob) and cancel it when it makes sense...bj0
12/26/2017, 9:47 PMlaunch { delay() }
dave08
12/26/2017, 9:58 PMbj0
12/26/2017, 10:05 PMlaunch{}
because .start()
is a suspend
function, and I just need to fire it off. I plan on cleaning up that structure later, but I'm currently working on dealing with BLE details atm so I just needed a quick launch
launch { delay() }
crashes, I expect it's a bug in the coroutine library