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 PMbj0
12/26/2017, 8:53 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}" }
// }
}
}
}
bj0
12/26/2017, 8:53 PMonCreateView
from a Fragment
bj0
12/26/2017, 8:53 PMdave08
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.dave08
12/26/2017, 9:23 PMlaunch
since it runs by default on CommonPool
context and not UI
dave08
12/26/2017, 9:26 PMlaunch
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 closedbj0
12/26/2017, 9:29 PMdave08
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)
}
}
dave08
12/26/2017, 9:40 PMval parentJob = Job()
in the Fragment, and running launch(CommonPool + parentJob) and cancel it when it makes sense...dave08
12/26/2017, 9:42 PMdave08
12/26/2017, 9:42 PMbj0
12/26/2017, 9:47 PMlaunch { delay() }
bj0
12/26/2017, 9:49 PMdave08
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
bj0
12/26/2017, 10:06 PMlaunch { delay() }
crashes, I expect it's a bug in the coroutine librarybj0
12/26/2017, 10:06 PM