Skip to content

NullPointerException when setting StateFlow value #3820

Closed
@hakonschia

Description

@hakonschia

Describe the bug

After upgrading from version 1.6.4 to 1.7.1 (we have since bumped to 1.7.2) we started seeing NullPointerException crashes when updating a StateFlow value. I am not able to reproduce this locally, we are only seeing this through Firebase reports.

This is happening on Android with org.jetbrains.kotlinx:kotlinx-coroutines-android. It's happening on many different devices and Android versions, so it isn't specific to one manufacturer/version.

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Class.isInterface()' on a null object reference
       at java.lang.Class.isAssignableFrom(Class.java:589)
       at java.lang.Class.isInstance(Class.java:542)
       at java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl.accessCheck(AtomicReferenceFieldUpdater.java:389)
       at java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl.get(AtomicReferenceFieldUpdater.java:447)
       at kotlinx.coroutines.flow.StateFlowSlot.makePending(StateFlowSlot.java:59)
       at kotlinx.coroutines.flow.StateFlowImpl.updateState(StateFlow.kt:349)
       at kotlinx.coroutines.flow.StateFlowImpl.setValue(StateFlow.kt:316)

Provide a Reproducer

We have a lot of flows, and only one specific one is crashing. The only unique thing about the flow that is causing crashes is that it holds an Enum, but I don't know if that's causing it.

The StateFlow is nullable, but the value set on it after initialization is never null. The code below isn't our production code, but is how our code looks like

enum class SomeEnum {
    Apple,
    Orange
}

class Class {
    val flow: StateFlow<SomeEnum?> get() = _mutableFlow
    private val _mutableFlow: MutableStateFlow<SomeEnum?> = MutableStateFlow(null)
    
    fun updateValue(value: SomeEnum) {
        _mutableFlow.value = value
    }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions