{"record":{"id":"d8dc88305416129e","repo":"google/gson","slug":"unable-to-create-instance-of-rawtype-register","errorCode":null,"errorMessage":"Unable to create instance of ${rawType}; Register an InstanceCreator or a TypeAdapter for this type.","messagePattern":"Unable to create instance of (.+?); Register an InstanceCreator or a TypeAdapter for this type\\.","errorType":"exception","errorClass":"JsonIOException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java","lineNumber":424,"sourceCode":"   * ObjectConstructor}, which would then choose another way of creating the object. And it supports\n   * types which are only serialized but not deserialized (compared to directly throwing an\n   * exception when the {@code ObjectConstructor} is requested), e.g. when the runtime type of an\n   * object is inaccessible, but the compile-time type is accessible.\n   */\n  private static final class ThrowingObjectConstructor<T> implements ObjectConstructor<T> {\n    private final String exceptionMessage;\n\n    ThrowingObjectConstructor(String exceptionMessage) {\n      this.exceptionMessage = exceptionMessage;\n    }\n\n    @Override\n    public T construct() {\n      // New exception is created every time to avoid keeping a reference to an exception with\n      // potentially long stack trace, causing a memory leak\n      // (which would happen if the exception was already created when the\n      // `ThrowingObjectConstructor` is created)\n      throw new JsonIOException(exceptionMessage);\n    }\n  }\n\n  private static final class InstanceCreatorConstructor<T> implements ObjectConstructor<T> {\n    private final InstanceCreator<T> instanceCreator;\n    private final Type type;\n\n    InstanceCreatorConstructor(InstanceCreator<T> instanceCreator, Type type) {\n      this.instanceCreator = instanceCreator;\n      this.type = type;\n    }\n\n    @Override\n    public T construct() {\n      return instanceCreator.createInstance(type);\n    }\n  }\n}","sourceCodeStart":406,"sourceCodeEnd":442,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java#L406-L442","documentation":"Thrown when the type has no accessible no-args constructor, no InstanceCreator, no special collection constructor, and allowUnsafe is false. This comes from ConstructorConstructor.get(typeToken, false), which disallows the Unsafe fallback. The message suggests registering an InstanceCreator or TypeAdapter.","triggerScenarios":"ConstructorConstructor.get(typeToken, false) is called for a concrete, non-abstract type that lacks a no-args constructor and InstanceCreator; the false allowUnsafe parameter prevents the Unsafe fallback.","commonSituations":"Internal Gson code paths that deliberately disallow Unsafe (e.g. specific deserializers calling the two-arg get); testing utilities; library code that wants strict construction semantics.","solutions":["Add a no-args constructor to the class","Register an InstanceCreator for the type","Register a custom TypeAdapter for the type","If you control the call, use the single-arg get(typeToken) which allows Unsafe"],"exampleFix":"// before - type has only a parameterized ctor, get(.., false)\nconstructorConstructor.get(typeToken, false);\n\n// after - register InstanceCreator or add no-args ctor\ngsonBuilder.registerTypeAdapter(MyType.class, myInstanceCreator);\n// OR\nclass MyType { public MyType() {} }","handlingStrategy":"validation","validationCode":"// If calling get(typeToken, false), ensure the type has a no-args ctor or InstanceCreator\nClass<?> c = typeToken.getRawType();\nboolean hasNoArgs;\ntry { c.getDeclaredConstructor(); hasNoArgs = true; }\ncatch (NoSuchMethodException e) { hasNoArgs = false; }\nif (!hasNoArgs && !hasInstanceCreator(c)) {\n  throw new IllegalStateException(c + \" needs no-args ctor or InstanceCreator (Unsafe disallowed)\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  return constructorConstructor.get(typeToken, false).construct();\n} catch (JsonIOException e) {\n  if (e.getMessage().startsWith(\"Unable to create instance\") && e.getMessage().contains(\"Register an InstanceCreator\")) {\n    throw new MyParseException(\"allowUnsafe=false requires no-args ctor for \" + typeToken, e);\n  }\n  throw e;\n}","preventionTips":["When using get(typeToken, false), ensure every target type has a no-args constructor or InstanceCreator","Register InstanceCreators for types that lack no-args constructors","Prefer the single-arg get(typeToken) unless you specifically want Unsafe-free construction"],"tags":["constructor","unsafe","instantiation","instance-creator"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}