{"id":"eba15655717d68b0","repo":"google/gson","slug":"unable-to-create-instance-of-rawtype-registerin","errorCode":null,"errorMessage":"Unable to create instance of {rawType}. Registering an InstanceCreator or a TypeAdapter for this type, or adding a no-args constructor may fix this problem.","messagePattern":"Unable to create instance of (.+?)\\. Registering an InstanceCreator or a TypeAdapter for this type, or adding a no-args constructor may fix this problem\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"critical","filePath":"gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java","lineNumber":369,"sourceCode":"    }\n    // Then try ConcurrentNavigableMap implementation\n    else if (rawType.isAssignableFrom(ConcurrentSkipListMap.class)) {\n      return ConcurrentSkipListMap::new;\n    }\n\n    // Was unable to create matching Map constructor\n    return null;\n  }\n\n  private <T> ObjectConstructor<T> newUnsafeAllocator(Class<? super T> rawType) {\n    if (useJdkUnsafe) {\n      return () -> {\n        try {\n          @SuppressWarnings(\"unchecked\")\n          T newInstance = (T) UnsafeAllocator.INSTANCE.newInstance(rawType);\n          return newInstance;\n        } catch (Exception e) {\n          throw new RuntimeException(\n              (\"Unable to create instance of \"\n                  + rawType\n                  + \". Registering an InstanceCreator or a TypeAdapter for this type, or adding a\"\n                  + \" no-args constructor may fix this problem.\"),\n              e);\n        }\n      };\n    } else {\n      String exceptionMessage =\n          \"Unable to create instance of \"\n              + rawType\n              + \"; usage of JDK Unsafe is disabled. Registering an InstanceCreator or a TypeAdapter\"\n              + \" for this type, adding a no-args constructor, or enabling usage of JDK Unsafe may\"\n              + \" fix this problem.\";\n\n      // Check if R8 removed all constructors\n      if (rawType.getDeclaredConstructors().length == 0) {\n        // R8 with Unsafe disabled might not be common enough to warrant a separate Troubleshooting","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/ConstructorConstructor.java#L351-L387","documentation":"When no no-args constructor and no InstanceCreator exist, Gson falls back to sun.misc.Unsafe.allocateInstance (if enabled). If that fails (UnsupportedOperationException on JVMs without sun.misc.Unsafe, module-access denial, or final-field issues) it wraps the cause as RuntimeException advising to register an InstanceCreator or TypeAdapter or add a no-args constructor.","triggerScenarios":"Deserializing a class with no no-args constructor on a JVM/manifest where Unsafe is blocked or unavailable (recent JDKs with --illegal-access=deny, GraalVM, some Android runtimes, modules sealing the package).","commonSituations":"Upgrading JDK (Unsafe restrictions); running on GraalVM/native-image; R8/ProGuard stripping constructors; record classes or classes with only required-args constructors.","solutions":["Add an accessible no-args constructor to the class.","Register an InstanceCreator<T> that returns a properly constructed instance.","Register a TypeAdapter<T> that builds the object via the existing required-args constructor.","If appropriate, allow JDK Unsafe via the GsonBuilder configuration or module opens."],"exampleFix":"// before\npublic final class User { /* only User(String, String) exists */ }\n\n// after (option A: InstanceCreator)\nGson gson = new GsonBuilder()\n    .registerTypeAdapter(User.class, (InstanceCreator<User>) t -> new User(\"\", \"\"))\n    .create();","handlingStrategy":"fallback","validationCode":"try {\n  rawType.getDeclaredConstructor(); // no-args constructor exists?\n} catch (NoSuchMethodException e) {\n  // must register InstanceCreator or TypeAdapter\n}\n","typeGuard":"boolean hasNoArgsConstructor(Class<?> c) {\n  try { c.getDeclaredConstructor(); return true; }\n  catch (NoSuchMethodException e) { return false; }\n}\n","tryCatchPattern":"try {\n  return gson.fromJson(json, type);\n} catch (RuntimeException ex) {\n  if (ex.getMessage().startsWith(\"Unable to create instance\")) {\n    // register InstanceCreator and retry\n  }\n  throw ex;\n}\n","preventionTips":["Add an accessible no-args constructor to deserialized classes.","Keep -keep rules for classes under R8/ProGuard.","Register InstanceCreator/TypeAdapter for record/required-args types.","Test deserialization on the target JDK (Unsafe availability varies)."],"tags":["deserialization","reflection","unsafe","instantiation","jdk-compat"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}