can someone help me with this one <https://hastebi...
# ktor
o
can someone help me with this one https://hastebin.com/uzetogurup.php ?
Line 56 is if(!input.readUTF8LineTo(stringBuilder)) throw Exception("BasicConnection.kt: Unknow error!")
g
No, I suppose this is wrong line
data
not initilised in case of error
so when you try to return it you get this crash
just rewrite your code. You don’t need lateinit
Copy code
val data =  try {
            var stringBuilder: StringBuilder = StringBuilder()
            if(!input.readUTF8LineTo(stringBuilder)) throw Exception("BasicConnection.kt: Unknow error!")
            data = stringBuilder.substring(0,stringBuilder.indexOf("\r\n"))
        } catch (e: Throwable) {
            e.printStackTrace()
            socket.close()
            connected = false
           null // or any other default value
        }
or even more simple solution, but not so idiomatic:
Copy code
var data: String
        try {
            data = "success"
        } catch(e: Throwable) {
            data = "error"
        }
        return data
o
ok thnx
fun receiveData() = runBlocking { var data = try { var stringBuilder: StringBuilder = StringBuilder() if(!input.readUTF8LineTo(stringBuilder)) throw Exception("BasicConnection.kt: Unknow error!") stringBuilder.substring(0,stringBuilder.indexOf("\r\n")) } catch (e: Throwable) { e.printStackTrace() socket.close() connected = false null } data }