Android hashmap kotlin中的空数据

Android hashmap kotlin中的空数据,android,kotlin,hashmap,Android,Kotlin,Hashmap,我想将数据插入到HashMap中。键是一个整数,值是一个字符串。值是从外部源接收的 我将收到的键和值放入Hashmap中,但当我尝试打印值时,得到的是null 为什么会这样 private var hashMap: HashMap<Int, String>? = null fun addData(key : Int, value : String) { hashMap?.put(key, value) println("********Ma

我想将数据插入到
HashMap
中。键是一个
整数
,值是一个
字符串
。值是从外部源接收的

我将收到的键和值放入
Hashmap
中,但当我尝试打印值时,得到的是
null

为什么会这样

private var hashMap: HashMap<Int, String>? = null

    fun addData(key : Int, value : String) {
        hashMap?.put(key, value)

        println("********Maaaaapphasshh = $hashMap")  //null why?

        hashMap?.forEach { (key, value ) ->
            println("********$key = $value ")       //null why?
        }
    }
private var hashMap:hashMap?=无效的
fun addData(键:Int,值:String){
hashMap?.put(键、值)
println(“******maaaapphashh=$hashMap”)//null为什么?
hashMap?.forEach{(键,值)->
println(“******$key=$value”)//null为什么?
}
}

hashMap
被创建为
null
,并且它从未被实例化:

private var hashMap: HashMap<Int, String>? = null
private var hashMap:hashMap?=无效的
尝试替换为:

private var hashMap: HashMap<Int, String>? = HashMap<Int, String> ()
private var hashMap:hashMap?=HashMap()

hashMap:hashMap?=null-->您将哈希映射创建为null,并且从不实例化它。。尝试替换为hashMap:hashMap?=哦,天哪,我真是太傻了。THX,仅在实际需要重新分配时才使用
var