I keep getting an error in kotlin that says “only safe and non-nullable asserted calls are allowed” and I can’t seem to figure it out

Heres the function I have, the purpose is to get the distance between 2 points and so I created the variables lat1-2 and long1-2 to get their respective coordinates in the function.(I have a handler that runs every 3 seconds to update lat and long2)

private fun getDistance(){
fusedLocationProviderClient.lastLocation.addOnCompleteListener { task ->
var location1 = task.result
if(location1 == null){
locationRequest = LocationRequest()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.interval = 0
locationRequest.fastestInterval = 0
locationRequest.numUpdates = 2
fusedLocationProviderClient!!.requestLocationUpdates(
locationRequest,locationCallback,Looper.myLooper())
var lat1 = location1.latitude
var long1:Double = location1.longitude
}else{
locationRequest = LocationRequest()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.interval = 0
locationRequest.fastestInterval = 0
locationRequest.numUpdates = 2
fusedLocationProviderClient!!.requestLocationUpdates(
locationRequest,locationCallback,Looper.myLooper())
var lat2:Double = location1.latitude
var long2: Double = location1.longitude
}
}
val distanceResult = FloatArray(1)
Location.distanceBetween(lat1,long1,lat2,lon2,distanceResult)
val distanceInMeters = distanceResult

Distancetxt.text = “Distance traveled: ” + distanceInMeters
}

The main function to get the long, lat, altitude, and speed works. And the functions name is getLocation. So I know the calls work and everything. But when I added the bolded stuff, It wouldnt let me assign the variables.

Hope you can guide me towards the right direction, and thanks in advance!

submitted by /u/MatroixBeats
[link] [comments]

Continue Reading I keep getting an error in kotlin that says “only safe and non-nullable asserted calls are allowed” and I can’t seem to figure it out

Doodle 0.5.0 released.

Doodle is a pure Kotlin UI framework for the Web, that lets you create rich applications without relying on Javascript, HTML or CSS. Check out the documentation and tutorials to learn more.

Highlights include

Multi-touch Support

Doodle now supports multiple pointers (i.e. touches) by default. So there is no additional work needed to enable this in apps.

  • PointerInputManager

    now tracks a collection of pointers and dispatches events to Views on a per-pointer basis.

  • PointerEvent

    now has information about the list of pointers (for the View and overall). This lets handlers support multi-touch.

  • Resizer

    now works better with multi-touch.

  • Added ability to suppress OS handling of PointerEvent. This is like consume, except it does not affect other app listeners.

Accessibility Support

This release stabilizes accessibility and brings a lot of new support to items in the Controls library.

Kotlin IR compiler Support

Apps can now choose to use either the IR or Legacy artifacts.

submitted by /u/pusolito
[link] [comments]

Continue Reading Doodle 0.5.0 released.

What do you think about upcoming namespace keyword?

Outside of classes, I can either declare functions as top level, or put them inside an

object

. Personally I really

object

, to me it just makes everything way more readable.

In this talk about the future of Kotlin it seems that they will address this with

namespace

.

The reasoning is that some people (like me) use

object

just to create a namespace, but don’t care creating an instance or a type of that object. To me, this just seems so perfect!

I don’t know much about how Kotlin releases, but I searched for “kotlin namespace keyword” and couldn’t find much about this topic. I was wondering when could I expect this feature to come, or if it’s any good. Are there downsides to it?

submitted by /u/cris_null
[link] [comments]

Continue Reading What do you think about upcoming namespace keyword?

I don’t understand Coroutines at all

Hello everyone,

I’ve been trying to convert my tcp server application to use coroutines. The current setup is basically creating a thread for every client that connects to the socket. Of course that’s not really scalable, so I first thought about creating a threadpool and do scheduling via callbacks, but then I wanted to try using coroutines instead, since they seem to be a good solution to my problem.

But even after now two weeks of experimenting I still don’t understand how to use them correctly in my case. This isn’t really helped by the fact that like 95% of online tutorials regarding them focus on Android UI development.

So my basic Setup is this:

My main functions starts two different Servers. They’re mostly decoupled from each other, but for performance reasons they’re still in the same applications so that they can easily communicate with each other via Channels.

Note: This is mostly just pseudo code atm. If it’s needed, I’ll update this to be more reflective of my actual implementation.

According to the documentation, this seems to be they correct way to do this:


fun main() = runBlocking { //initialize database connections and other stuff ... launch { startServer1() } launch { startServer2() } }

Now in e.g. Server1 I want to do an accept loop, which I imagine should look like this:


suspend fun startServer1() { val socket = createSocket() while(true) { val client = socket.accept() launch { handleUser(client) } } }

FYI: I’m using ktor sockets.

But now I’m already at a blocker: I need a coroutineScope to use launch. Am I supposed to pass

this

in main to the startServer functions? But what if I want to use a threadpool? Seems like I can create one with

Executors.newFixedThreadPool(someNumber).asCoroutineDispatcher()

. But what if I wanted to use this again somewhere further down the stack? Should I create a global context variable and then instead of

launch

call

withContext(myThreadPool)

? Or should all my functions that would want to call launch take the callers context as an argument?

Or am I not supposed to use

launch

et al and just run everything with normal function calls? But that would obviously block (I tested this by using a

delay()

in the

handleUser()

function).

I’m honestly at my wits end. Are coroutines maybe just not made for this scenario? Am I fundamentally misunderstanding how they work? I just want to have a threadpool that transparently schedules all my connected clients.

Any help would be appreciated.

submitted by /u/weeblay
[link] [comments]

Continue Reading I don’t understand Coroutines at all

How Do You Use Stack Overflow? The Kotlin Community Survey

Stack Overflow is an essential resource when it comes to learning something new about programming. It is easily searchable, used by millions of people, and extremely popular in the software engineering community. During the Kotlin Census 2020, 55% of respondents mentioned Stack Overflow as a helpful learning resource.

However, sometimes we hear that finding Kotlin-related information on Stack Overflow is not easy. Given the importance of the platform, we’re keen to fix this, and increase the number of questions answered there.

It will help us a lot if you could share how you search for Kotlin-related information on Stack Overflow by filling out our survey.

Fill out the survey

The survey will take around 10 minutes to complete. As a thank you for your help, you will have a chance to win a JetBrains All Product Pack subscription or a $100 Amazon eGift voucher. Winners will be picked at random from among the surveys that are filled out completely.

If you are interested in helping us with our research, please share your email and check the box at the end of the survey. We will keep you posted about our research and how you can participate in future activities through the newsletter.

Continue Reading How Do You Use Stack Overflow? The Kotlin Community Survey

End of content

No more pages to load