{"record":{"id":"417d64ee227e2d47","repo":"apple/pkl","slug":"expectednonnullvalue","errorCode":"expectedNonNullValue","errorMessage":"expectedNonNullValue","messagePattern":"expectedNonNullValue","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/expression/unary/NonNullNode.java","lineNumber":41,"sourceCode":"import org.pkl.core.runtime.VmNull;\n\n@NodeInfo(shortName = \"!!\")\n// Truffle DSL/codegen is overkill for this node, hence don't extend UnaryExpressionNode\npublic final class NonNullNode extends ExpressionNode {\n  private @Child ExpressionNode operandNode;\n\n  public NonNullNode(SourceSection sourceSection, ExpressionNode operandNode) {\n    super(sourceSection);\n    this.operandNode = operandNode;\n  }\n\n  @Override\n  public Object executeGeneric(VirtualFrame frame) {\n    var operand = operandNode.executeGeneric(frame);\n    if (!(operand instanceof VmNull)) return operand;\n\n    CompilerDirectives.transferToInterpreter();\n    throw exceptionBuilder()\n        .evalError(\"expectedNonNullValue\")\n        .withSourceSection(operandNode.getSourceSection())\n        .build();\n  }\n}\n","sourceCodeStart":23,"sourceCodeEnd":47,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/expression/unary/NonNullNode.java#L23-L47","documentation":"Thrown by the non-null assertion operator `!!` when its operand evaluates to null. The error is reported at the operand's source section so the developer can see which expression was null. Pkl's `!!` is a deliberate runtime assertion that a value is non-null.","triggerScenarios":"Evaluating `x!!` where `x` is null — e.g. an optional property that was never set, a `read()` on a missing resource returning null, or a Map/List lookup producing null followed by `!!`.","commonSituations":"Asserting non-null on property lookups that may be absent, parsing external data with optional fields, using `!!` defensively instead of providing defaults.","solutions":["Look at the highlighted operand expression; check why it evaluates to null.","Replace `!!` with an explicit null check or use `??` to supply a default value.","If the operand is an absent property, ensure the upstream module actually defines/sets it.","Use `isNonNullable`/instanceof tests to guard before applying `!!`."],"exampleFix":"// before\nval port = externalConfig?.port!!\n// after\nval port = externalConfig?.port ?? 8080","handlingStrategy":"type-guard","validationCode":"val port = if (cfg?.port != null) cfg.port else 8080","typeGuard":"function requireNonNull<T>(v: T|null, msg: String): T =\n  if (v == null) throw new Error(msg) else v","tryCatchPattern":"try {\n  value = operand!!\n} catch (e) {\n  // e.code === 'expectedNonNullValue': operand was null; supply default\n}","preventionTips":["Prefer `??` defaults over bare `!!`","Check optional properties with `!= null` before asserting","Avoid `!!` on values derived from external input"],"tags":["null","assertion","non-null-operator"],"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"}