简体   繁体   中英

Null Pointer Exception working with Map (Kotlin)

I have the following class:

class SymbolTable(){
    var map = mutableMapOf<String, Entry>()
    var kindCounter = mutableMapOf<String, Int>()

    fun define(name:String, kind:String, type:String){
        if(kindCounter[kind]==0){
            kindCounter[kind]=0
        }
        var index = 1
        map[name]= Entry(type, kind, index)
        kindCounter[kind]=kindCounter[kind]!!.plus(1)
    }

class Entry looks like this:

class Entry(var type:String, var kind:String, var index:Int)

Main:

fun main(args:Array<String>){
    var example = SymbolTable()
    example.define("ex1", "ex1", "ex1")
    example.define("ex2", "ex2", "ex2")
}

When I run the program and try using the "define" function I get the following error:

Exception in thread "main" kotlin.KotlinNullPointerException
    at SymbolTable.define(SymbolTable.kt:21)
    at SymbolTableKt.main(SymbolTable.kt:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

I assume the problem has something to do with how I create a new symbolTable class, but seeing as Kotlin doesn't have "new" I don't know how to avoid the null pointer exception.

if(kindCounter[kind]==0){
   kindCounter[kind]=0
}

This doesn't make much sense: you test if the value is 0, and if it is, you set it to 0. So it's basically a noop.

What you want is to test if the value is null:

if (kindCounter[kind] == null) {
   kindCounter[kind] = 0
}

You could also avoid using the dangerous !! operator by saving the value into a variable.

And you should really use val rather than var: all of your fields shouldn't be mutable:

class SymbolTable() {
    val map = mutableMapOf<String, Entry>()
    val kindCounter = mutableMapOf<String, Int>()

    fun define(name: String, kind: String, type: String) {
        val count = kindCounter[kind] ?: 0
        map[name] = Entry(type, kind, 1)
        kindCounter[kind] = count + 1
    }
}

class Entry(val type: String, val kind: String, val index: Int)

fun main(args:Array<String>) {
    val example = SymbolTable()
    example.define("ex1", "ex1", "ex1")
    example.define("ex2", "ex2", "ex2")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM