My code works 9,999 out of 10,000 times. But, for about once in every 10,000 times, the application crashes to a UninitializedPropertyAccessException This is problematic as there are about 20,000~30,000 Android devices with my code in production.
It appears that, sometimes, the actual assignment of the lateinit variable is not happening soon enough (and hence causing the exception above).
Has anybody else had a similar problem? What was your solution?
TcpService.kt
class TcpService: Service() { private lateinit var mTcpClient: TcpClient
override fun onCreate() {
registerReceiver(object: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when(intent.action) {
// This will cause an Uninitialized Property Exception 1 in 10,000 times
ACTION_SEND_MESSAGE -> mTcpClient.sendMessageAsync(intent.getStringExtra(EXTRA_NEW_MESSAGE)
}
}
}, IntentFilter(ACTION_SEND_MESSAGE)
})
}
override fun onStart() {
mTcpClient = mTcpClient()
}
// ...
}
TcpClient.kt
class TcpClient() {
init {
// ...
sendBroadcast(Intent(ACTION_SEND_MESSAGE).putExtra(EXTRA_NEW_MESSAGE, "Message Contents")
}
// ...
}
ahh it happened to me once and i wasted whole day then i solved it(still dont know how) but why dont you make
mTcpClientnull and then null check:something like that im not a professional sorry