{"record":{"id":"40c0745276410d0d","repo":"google/gson","slug":"null-is-not-allowed-as-value-for-record-component","errorCode":null,"errorMessage":"null is not allowed as value for record component '${fieldName}' of primitive type; at path ${path}","messagePattern":"null is not allowed as value for record component '(.+?)' of primitive type; at path (.+?)","errorType":"exception","errorClass":"JsonParseException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java","lineNumber":262,"sourceCode":"          fieldValue = field.get(source);\n        }\n\n        @SuppressWarnings(\"ReferenceEquality\")\n        boolean isSameObject = fieldValue == source;\n        if (isSameObject) {\n          // avoid direct recursion\n          return;\n        }\n        writer.name(serializedName);\n        writeTypeAdapter.write(writer, fieldValue);\n      }\n\n      @Override\n      void readIntoArray(JsonReader reader, int index, Object[] target)\n          throws IOException, JsonParseException {\n        Object fieldValue = typeAdapter.read(reader);\n        if (fieldValue == null && isPrimitive) {\n          throw new JsonParseException(\n              \"null is not allowed as value for record component '\"\n                  + fieldName\n                  + \"' of primitive type; at path \"\n                  + reader.getPath());\n        }\n        target[index] = fieldValue;\n      }\n\n      @Override\n      void readIntoField(JsonReader reader, Object target)\n          throws IOException, IllegalAccessException {\n        Object fieldValue = typeAdapter.read(reader);\n        if (fieldValue != null || !isPrimitive) {\n          if (blockInaccessible) {\n            checkAccessible(target, field);\n          } else if (isStaticFinalField) {\n            // Reflection does not permit setting value of `static final` field, even after calling\n            // `setAccessible`","sourceCodeStart":244,"sourceCodeEnd":280,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java#L244-L280","documentation":"When deserializing JSON into a record with a primitive-typed component (int, long, boolean, etc.), Gson reads each value into a constructor argument array. A JSON null cannot be assigned to a primitive slot, so readIntoArray throws JsonParseException before the constructor is ever called.","triggerScenarios":"Deserializing JSON containing a null literal for a field mapped to a primitive record component, e.g. {\"age\": null} into record Person(int age).","commonSituations":"A backend API returns null for a field the client modeled as a primitive int/long/boolean in a record; schema evolution where a previously-non-null field becomes nullable.","solutions":["Change the primitive record component to its boxed wrapper type (Integer, Long, Boolean) so null is accepted","Pre-process the JSON to replace null values with sensible defaults before calling fromJson","Register a custom TypeAdapter that substitutes a default value (e.g. 0) when the JSON value is null"],"exampleFix":"// before\nrecord Person(int age) {}\ngson.fromJson(\"{\\\"age\\\":null}\", Person.class); // throws\n\n// after\nrecord Person(Integer age) {}\ngson.fromJson(\"{\\\"age\\\":null}\", Person.class); // age is null, no error","handlingStrategy":"validation","validationCode":"// Before deserializing, scan the JSON for nulls on primitive record components\npublic static void checkPrimitiveNulls(JsonElement json, Class<?> recordType) {\n    var components = recordType.getRecordComponents();\n    var obj = json.getAsJsonObject();\n    for (var c : components) {\n        if (c.getType().isPrimitive() && obj.has(c.getName())\n                && obj.get(c.getName()).isJsonNull()) {\n            throw new IllegalStateException(\n                \"JSON null for primitive record component: \" + c.getName());\n        }\n    }\n}","typeGuard":"// Prefer boxed types for nullable components\nrecord Person(Integer age) {} // Integer accepts null; int does not","tryCatchPattern":"try {\n    Person p = gson.fromJson(json, Person.class);\n} catch (JsonParseException e) {\n    if (e.getMessage().contains(\"null is not allowed\")) {\n        // default the primitive or use a nullable wrapper\n    }\n}","preventionTips":["Use boxed wrapper types (Integer, Long, Boolean) for record components that may be null in JSON","Validate the JSON payload for nulls on primitive fields before calling fromJson","Document which record components are nullable vs required"],"tags":["deserialization","record","primitive","null"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}