https://kotlinlang.org logo
#compose
Title
# compose
a

Alexander Maryanovsky

05/10/2022, 7:13 AM
Getting a
ClassCastException
here. Seems like a Compose compiler/runtime bug.
1
Copy code
class Car(
    val model: String
)

class Person(
    val name: String,
    val car: MutableStateFlow<Car?> = MutableStateFlow(null)
)


val people = mutableListOf<MutableStateFlow<Person?>>(
    MutableStateFlow(Person("Aristotle")),
    MutableStateFlow(Person("Ford", MutableStateFlow(Car("Model T")))),
    MutableStateFlow(Person("Newton"))
)

@Composable
fun PersonUi(person: Person?){
    if (person == null)
        Text("No person")
    else
        Text(person.name)
}

fun main() {
    singleWindowApplication(
        title = "Test",
        state = WindowState(
            width = 600.dp,
            height = 800.dp,
        )
    ) {
        Column {
            for (index in (0 until people.size)){
                val person = people[index].collectAsState().value
                PersonUi(person)
                if (person != null){
                    val car by person.car.collectAsState()
                    car?.let {
                        Text(it.model)
                    }
                }
            }

            Spacer(
                modifier = Modifier.height(16.dp)
            )

            Button(
                onClick = {
                    people[1].value = null
                }
            ){
                Text("Remove Ford")
            }
        }
    }
}
Copy code
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class aod.Car cannot be cast to class aod.Person (aod.Car and aod.Person are in unnamed module of loader 'app')
	at aod.ComposableSingletons$MainKt$lambda-4$1.invoke(Main.kt:185)
	at aod.ComposableSingletons$MainKt$lambda-4$1.invoke(Main.kt:181)
	at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116)
	at androidx.compose.runtime.internal.ComposableLambdaImpl$invoke$1.invoke(ComposableLambda.jvm.kt:127)
	at androidx.compose.runtime.internal.ComposableLambdaImpl$invoke$1.invoke(ComposableLambda.jvm.kt:127)
	at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:140)
	at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2158)
	at androidx.compose.runtime.ComposerImpl.skipCurrentGroup(Composer.kt:2404)
	at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2585)
	at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:2571)
	at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:247)
	at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source)
	at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:2571)
	at androidx.compose.runtime.ComposerImpl.recompose$runtime(Composer.kt:2547)
	at androidx.compose.runtime.CompositionImpl.recompose(Composition.kt:620)
	at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:786)
	at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:105)
	at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:456)
	at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:425)
	at androidx.compose.runtime.BroadcastFrameClock$FrameAwaiter.resume(BroadcastFrameClock.kt:42)
	at androidx.compose.runtime.BroadcastFrameClock.sendFrame(BroadcastFrameClock.kt:71)
	at androidx.compose.ui.ComposeScene.render(ComposeScene.skiko.kt:379)
	at androidx.compose.ui.awt.ComposeLayer$1$onRender$1.invoke(ComposeLayer.desktop.kt:236)
	at androidx.compose.ui.awt.ComposeLayer$1$onRender$1.invoke(ComposeLayer.desktop.kt:235)
	at androidx.compose.ui.awt.ComposeLayer.catchExceptions(ComposeLayer.desktop.kt:88)
	at androidx.compose.ui.awt.ComposeLayer.access$catchExceptions(ComposeLayer.desktop.kt:69)
	at androidx.compose.ui.awt.ComposeLayer$1.onRender(ComposeLayer.desktop.kt:235)
	at org.jetbrains.skiko.SkiaLayer.update$skiko(SkiaLayer.awt.kt:455)
	at org.jetbrains.skiko.redrawer.MetalRedrawer.update(MetalRedrawer.kt:66)
	at org.jetbrains.skiko.redrawer.MetalRedrawer.access$update(MetalRedrawer.kt:13)
	at org.jetbrains.skiko.redrawer.MetalRedrawer$frameDispatcher$1.invokeSuspend(MetalRedrawer.kt:38)
	at org.jetbrains.skiko.redrawer.MetalRedrawer$frameDispatcher$1.invoke(MetalRedrawer.kt)
	at org.jetbrains.skiko.redrawer.MetalRedrawer$frameDispatcher$1.invoke(MetalRedrawer.kt)
	at org.jetbrains.skiko.FrameDispatcher$job$1.invokeSuspend(FrameDispatcher.kt:33)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
When I press the “Remove Ford” button
32 Views