Why am I getting a java.lang.ClassCastException in Kotlin?

2022-07-12T22:02:36

I wrote this sample code that I extracted from a library that I'm making because it is causing a java.lang.ClassCastException and I don't understand why. Here's the code:

interface Event

data class Update(val message: Message?)
open class Message(open val text: String?)
data class TextMessage(override val text: String) : Message(text = text)

class MessageReceiveEvent(
    val message: Message,
) : Event

class TextMessageReceiveEvent(
    val message: TextMessage,
) : Event

fun handleUpdate(update: Update): Event {
    if (update.message != null) {
        if (update.message.text != null) return TextMessageReceiveEvent(update.message as TextMessage)
        return MessageReceiveEvent(update.message)
    }
    error("Update message is null.")
}

handleUpdate(
    Update(
        message = Message(
            text = "Text" // if this is null, the program successfully runs.
        )
    )
).let {
    if (it is MessageReceiveEvent) println("Message without text.")
    if (it is TextMessageReceiveEvent) println("Message with text.")
}

Copyright License:
Author:「Dj Sushi」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/72953632/why-am-i-getting-a-java-lang-classcastexception-in-kotlin

About “Why am I getting a java.lang.ClassCastException in Kotlin?” questions

I wrote this sample code that I extracted from a library that I'm making because it is causing a java.lang.ClassCastException and I don't understand why. Here's the code: interface Event data class
I am trying to write a function to find the majority element in kotlin but when I compile the code I get variable expected error on the following line map[key]?.let{ value-> ...
val me = "Hello I am learning Kotlin" fun main(){ println(me[1,10]) } I am getting "Too many arguments for public open fun get(index: Int): Char defined in kotlin.String" e...
I am trying to write JUnit test in kotlin for the function which has get request. And I am getting 401 error. I found some solutions but all of them for Java, how can I solve this error in kotlin?
I'm writing a plugin for Intellij Idea, where the main idea is to get the structure of a Kotlin class as: properties, methods, etc. In my plugin.xml I added the following line <depends>org.
Can you tell me why my code isn't working? It used to be working fine and now that I'm changing over from listView to recyclerView I'm getting: java.lang.ClassCastException: java.lang.Integer can...
It seems this should be safe to do? @Test fun testCast() { val storeId : Any = Int.MAX_VALUE val numericStoreId = if(storeId is String) storeId.toLong() else storeId as Long } This yields:...
I am your classic programmer that learned in Java for app dev and is now trying to switch over to Kotlin. Right now I am just going through the tutorial that Android Developer has. On that page tho...
I am trying to implement android navigation drawer which is a part of navigation UI but while implementing it via kotlin i am getting an error that says unresolved error. class HomeActivity :
Kotlin builds fine locally. However, it does not work properly on the server. Below is the error log. /root/.sdkman/candidates/kotlin/current/bin/kotlinc: line 80: 34 Killed "${JAVACMD:=java...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.