{"record":{"id":"85c818de23606ded","repo":"apple/pkl","slug":"type-constraint-violation-value-must-not-be-null","errorCode":null,"errorMessage":"type constraint violation: value must not be null (non-null type alias)","messagePattern":"type constraint violation: value must not be null \\(non-null type alias\\)","errorType":"exception","errorClass":"VmTypeMismatchException","httpStatus":null,"severity":"error","filePath":"pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java","lineNumber":2500,"sourceCode":"      return new PType.TypeVariable(typeParameter);\n    }\n\n    @Override\n    protected boolean acceptTypeNode(boolean visitTypeArguments, TypeNodeConsumer consumer) {\n      return consumer.accept(this);\n    }\n  }\n\n  public static final class NonNullTypeAliasTypeNode extends WriteFrameSlotTypeNode {\n    public NonNullTypeAliasTypeNode() {\n      super(VmUtils.unavailableSourceSection());\n    }\n\n    @Override\n    protected Object executeLazily(VirtualFrame frame, Object value) {\n      if (value instanceof VmNull) {\n        CompilerDirectives.transferToInterpreterAndInvalidate();\n        throw constraintException(value, BaseModule.getNonNullTypeAlias().getConstraintSection());\n      }\n      return value;\n    }\n\n    @Override\n    public VmTypeAlias getVmTypeAlias() {\n      return BaseModule.getNonNullTypeAlias();\n    }\n\n    @Override\n    public VmTyped getMirror() {\n      return MirrorFactories.typeAliasTypeFactory.create(this);\n    }\n\n    @Override\n    public boolean doIsEquivalentTo(TypeNode other) {\n      return other instanceof NonNullTypeAliasTypeNode;\n    }","sourceCodeStart":2482,"sourceCodeEnd":2518,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java#L2482-L2518","documentation":"NonNullTypeAliasTypeNode.executeLazily: a value checked against a non-null type alias is null (VmNull), violating the alias's nullability constraint, so a type-constraint violation is thrown. The input at fault is a null value flowing into a position typed by a non-null type alias (e.g. `x: NonNull<Int?>`-style alias).","triggerScenarios":"A property or expression typed with the non-null type alias (`x: NonNull<T>` or via a user non-null alias) receives null at runtime during lazy evaluation.","commonSituations":"Defaults that resolve to null; chained lookups returning null (`foo?.bar` where foo is null); optional bindings that were expected to be non-null but a parent object omitted them.","solutions":["Ensure the value is non-null before assigning it to the non-null-aliased property.","Relax the type alias (drop the non-null constraint) if null is legitimate."],"exampleFix":"// before\nx: NonNull<Int> = maybeValue // maybeValue may be null\n\n// after\nx: NonNull<Int> = maybeValue ?? 0","handlingStrategy":"validation","validationCode":"if (value != null) { /* safe to bind to NonNull alias */ }","typeGuard":"function nonNull<T>(v: T?): Boolean = v != null","tryCatchPattern":null,"preventionTips":["Default optional values with `?? fallback` before binding to NonNull.","Avoid `?.` chains feeding NonNull-typed properties without null handling.","Ensure parent objects define required properties."],"tags":["pkl","null","type-constraint","nonnull"],"backgroundTag":"null-argument","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}