{"record":{"id":"d0aae23de233a602","repo":"apple/pkl","slug":"expected-a-non-null-value-but-got-null-to-allow","errorCode":null,"errorMessage":"Expected a non-null value but got `null`. To allow null values, convert to a nullable Kotlin type, for example `String?`.","messagePattern":"Expected a non-null value but got `null`\\. To allow null values, convert to a nullable Kotlin type, for example `String\\?`\\.","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"pkl-config-kotlin/src/main/kotlin/org/pkl/config/kotlin/ConfigExtensions.kt","lineNumber":48,"sourceCode":"/**\n * Converts this [Config] node to type [T] using the configured\n * [org.pkl.config.java.mapper.ValueMapper].\n *\n * To allow `null` values, specify a nullable type, for example `to<String?>()`.\n *\n * Kotlin code should prefer this method over [Config.as] for the following reasons:\n * * does not clash with Kotlin's `as` keyword\n * * throws [ConversionException] if conversion to non-nullable type returns `null`\n * * easier to use with parameterized types: `to<List<String>>()` vs.\n *   `as(JavaType.listOf(String::class.java))`\n */\ninline fun <reified T> Config.to(): T {\n  if (null is T) {\n    return asNullable(typeOf<T>().javaType)\n  }\n\n  return `as`(typeOf<T>().javaType)\n    ?: throw ConversionException(\n      \"Expected a non-null value but got `null`. \" +\n        \"To allow null values, convert to a nullable Kotlin type, for example `String?`.\"\n    )\n}\n\n/**\n * Configures this [ValueMapperBuilder] with conversions and converter factories for Kotlin types.\n */\nfun ValueMapperBuilder.forKotlin(): ValueMapperBuilder =\n  addConversions(KotlinConversions.all).addConverterFactories(KotlinConverterFactories.all)\n\n/**\n * Configures this [ConfigEvaluatorBuilder] with conversions and converter factories for Kotlin\n * types.\n */\nfun ConfigEvaluatorBuilder.forKotlin(): ConfigEvaluatorBuilder =\n  setValueMapperBuilder(valueMapperBuilder.forKotlin())\n","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-config-kotlin/src/main/kotlin/org/pkl/config/kotlin/ConfigExtensions.kt#L30-L66","documentation":"Config.to<T>() converts a Pkl Config value into a Kotlin type T. This error means the Pkl value is null but the requested Kotlin type T is non-nullable, so the conversion cannot proceed. The library deliberately tells you the fix: make T nullable (e.g. String?) to accept null.","triggerScenarios":"Calling config.to<String>() (or to<Foo>()) where the underlying Pkl property's value is null (e.g. `foo: String?` in Pkl set to null, or an амended property nulling it).","commonSituations":"Pkl schema allows a nullable field that is left null; an amendment sets a field to null; caller assumes required fields and uses non-nullable Kotlin types.","solutions":["Make the target Kotlin type nullable: config.to<String?>() instead of config.to<String>().","If the field should never be null, fix the Pkl config/amendment so the property has a value.","Provide a default before conversion: config[\"prop\"]?.let { it.to<String>() } ?: fallback (check null on the Config view first)."],"exampleFix":"// before\nval name: String = config.to<String>()\n// after\nval name: String? = config.to<String?>()","handlingStrategy":"type-guard","validationCode":"if (config[\"name\"] == null) error(\"config.name is null; provide a value or use String?\")","typeGuard":"inline fun <reified T> Config.toOrNull(): T? =\n  if (null is T) asNullable(typeOf<T>().javaType) else `as`(typeOf<T>().javaType)","tryCatchPattern":"val name = try {\n  config.to<String>()\n} catch (e: ConversionException) {\n  null // or default value\n}","preventionTips":["Use nullable Kotlin types (String?) for Pkl properties declared nullable.","Give Pkl properties non-null defaults so nulls only appear when intentional.","Run `pkl eval` validation on config files before loading them in Kotlin.","Grep your Pkl schemas for `?` typed properties and mirror nullability in Kotlin call sites."],"tags":["kotlin","nullability","conversion"],"backgroundTag":"null-argument","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}