Norm
Norm

Reputation: 1036

How can Kotlin fail to find a value in a LinkedHashMap when one of the keys returns true for equals?

I have a secondaryOutputProperties map,

 private val secondaryOutputProperties = mutableMapOf<URI, MutableMap<QName, XdmValue>>()

And I have a uri: URI that actually comes from the keys of another map.

I'm getting an inexplicable NPE. Consider this code:

            for ((puri, _) in secondaryOutputProperties) {
                println("Map URI: $puri, ${puri.equals(uri)}")
            }
            println("Key URI: $uri")
            val x = secondaryOutputProperties[uri]
            println("s[uri] = ${x}")
            val y = secondaryOutputProperties.get(uri)
            println("s.get(uri) = ${y}")
            println("in? ${uri in secondaryOutputProperties}")
            println("conains? ${secondaryOutputProperties.containsKey(uri)}")

The output is:

Map URI: file:/Volumes/Projects/xproc/test-suite/test-suite/tests/secondary-false, true
Map URI: file:/Volumes/Projects/xproc/test-suite/test-suite/tests/secondary-true, false
Key URI: file:/Volumes/Projects/xproc/test-suite/test-suite/tests/secondary-false
s[uri] = null
s.get(uri) = null
in? false
conains? false

The next statement in the method is:

            val serprops = secondaryOutputProperties[uri]!!

And that throws an NPE.

This is a single threaded program, so there's no other thread removing keys from the map or anything like that. How can puri.equals(uri) return true, but get() fail to find the value?

I assume I'm overlooking something obvious, but I don't see it...

Upvotes: 1

Views: 115

Answers (1)

Norm
Norm

Reputation: 1036

It was a multithreading bug. #blush

My code: single thread. Third party library: not so much.

Apologies for the noise.

Upvotes: 0

Related Questions