{"record":{"id":"5d4304ead0889ec6","repo":"apple/pkl","slug":"error-invoking-constructor-s","errorCode":null,"errorMessage":"Error invoking constructor `%s`.","messagePattern":"Error invoking constructor `(.+?)`\\.","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"pkl-config-java/src/main/java/org/pkl/config/java/mapper/PObjectToDataObject.java","lineNumber":228,"sourceCode":"        } catch (ConversionException e) {\n          throw new ConversionException(\n              String.format(\n                  \"Error converting property `%s` in Pkl object of type `%s` \"\n                      + \"to equally named constructor parameter in Java class `%s`: \"\n                      + e.getMessage(),\n                  param.first,\n                  value.getClassInfo(),\n                  Reflection.toRawType(targetType).getTypeName()),\n              e.getCause());\n        }\n      }\n\n      try {\n        @SuppressWarnings(\"unchecked\")\n        var result = (T) constructorHandle.invokeWithArguments(args);\n        return result;\n      } catch (Throwable t) {\n        throw new ConversionException(\n            String.format(\"Error invoking constructor `%s`.\", constructorHandle), t);\n      }\n    }\n  }\n}\n","sourceCodeStart":210,"sourceCodeEnd":234,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-config-java/src/main/java/org/pkl/config/java/mapper/PObjectToDataObject.java#L210-L234","documentation":"After all constructor arguments are converted, pkl-config-java invokes the target constructor via a MethodHandle (invokeWithArguments). If the invocation throws any Throwable — e.g. the constructor's own validation code threw, arguments are wrong after conversion, or the handle's type doesn't match the arguments — it is wrapped in this ConversionException with the constructor handle in the message.","triggerScenarios":"Converter.convert reaching constructorHandle.invokeWithArguments(args) where the constructor body throws (IllegalArgumentException on validation, NPE, custom checks), or argument arity/types mismatch the MethodHandle signature (e.g. null passed to a primitive parameter).","commonSituations":"Java constructor performs validation (e.g. requires port > 0) and the Pkl config violates it; null Pkl value mapped into a primitive parameter; class initialization failure (ExceptionInInitializerError); incompatible cached converter after hot code replacement.","solutions":["Inspect the cause chain (getCause()) — the real exception is thrown by your constructor body; fix the constructor or the offending config value.","Fix the Pkl value so it passes the constructor's validation (e.g. valid ranges, non-null fields).","If a null is being passed to a primitive parameter, change the Java parameter to a wrapper type or give the Pkl property a non-null default.","Ensure the converted class and its dependencies load cleanly (no static initializer failures, consistent class versions)."],"exampleFix":"// before: constructor rejects config value\npublic Server(int port) {\n  if (port <= 0) throw new IllegalArgumentException(\"port must be > 0\");\n  ...\n}\n// config: port = -1\n\n// after\n// config: port = 8080","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  return converter.convert(pklObject, valueMapper);\n} catch (ConversionException e) {\n  if (e.getMessage().startsWith(\"Error invoking constructor\")) {\n    Throwable root = e;\n    while (root.getCause() != null) root = root.getCause();\n    throw new ConfigLoadException(\"Constructor rejected config values: \" + root, root);\n  }\n  throw e;\n}","preventionTips":["Unwrap and log getCause() to see the real constructor failure","Avoid constructors with side-effecting validation for config-mapped classes, or keep validation messages descriptive","Ensure null Pkl values never map to primitive parameters (use wrapper types or non-null defaults)","Cover constructor validation rules with tests against representative config values"],"tags":["reflection","methodhandle","constructor-invocation","java","pkl"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}