Skip to content

LDContext.builderFromContext() throws NPE when trying to set a custom attribute #229

@gzhu

Description

@gzhu

Is this a support request?
No

Describe the bug
When updating the LDContext using builderFromContext(), if the original context doesn't have any custom attributes, trying to set a custom attribute gives a null pointer error. The copyOnWriteAttributes flag gets set and seems to cause a HashMap to be constructed from a null hashmap attributes = new HashMap<>(attributes);

To reproduce
I started with a plain context:

ldContext = LDContext.create(UUID.randomUUID().toString())

and then later when I need to update the context:

val updatedContext: LDContext = LDContext.builderFromContext(ldContext)
            .anonymous(false)
            .name(name)
            .set("email", email)
            .build()

It crashes on the .set("email", email) call

Expected behavior
It should not crash

Logs

java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.Map.size()' on a null object reference
                                                                                                    	at java.util.HashMap.putMapEntries(HashMap.java:493)
                                                                                                    	at java.util.HashMap.<init>(HashMap.java:482)
                                                                                                    	at com.launchdarkly.sdk.ContextBuilder.trySet(ContextBuilder.java:306)
                                                                                                    	at com.launchdarkly.sdk.ContextBuilder.set(ContextBuilder.java:212)
                                                                                                    	at com.launchdarkly.sdk.ContextBuilder.set(ContextBuilder.java:261)

SDK version
5.0.2

Language version, developer tools
Kotlin 1.8.21

OS/platform
Android API 34

Additional context
The HashMap constructor provided by android sdk34 seems to check the existing map's size when it is passed in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions