Search code examples
androidsqlitekotlinanko

Type mismatch. Required Map<String, Any> Found Map<String, Any?>


I am trying to execute select using anko extension in kotlin like below

fun read() {
    database.use {
        select(PersonTable.Name).exec {
            select("myTable").exec() {
                parseList(
                    object : MapRowParser<Map<String, Any>> {
                        override fun parseRow(columns: Map<String, Any?>): Map<String, Any> {
                            Log.d("Hello", "Hello")
                            return columns;
                        }
                    }
                )
            }
        }
    }
}

I am getting an error on return column

Type mismatch. Required Map<String, Any> Found Map<String, Any?>

If I change to override fun parseRow(columns: Map<String, Any>): Map<String, Any> then it shows an error.

enter image description here

build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.williams.fourthdemo"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile "org.jetbrains.anko:anko-common:0.10.1"
    compile "org.jetbrains.anko:anko-sqlite:0.10.1"
}

Is this bug in the anko library ?


Solution

  • Looks like

    object : MapRowParser<Map<String, Any>> {

    should really be

    object : MapRowParser<Map<String, Any?>> {

    It is very common for databases to have columns that return a null in the cell. The way you defined it as Any means null is never going to happen. If you cast your return to a Map<String, Any> then you will simply end up with runtime errors happening if a null is contained anywhere.

    You are best suited by making it an Any? type which forces the parseRow return type to be Map<String, Any?> as well and fixes the potential bug in your code that would happen if a null value were to creep up somewhere.