{"id":"50e7789627495fad","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 \" + reader.getPath()","messagePattern":"null is not allowed as value for record component '\" \\+ fieldName \\+ \"' of primitive type; at path \" \\+ reader\\.getPath\\(\\)","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/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java#L244-L280","documentation":"Thrown while deserializing a Java record when a JSON null is encountered for a component whose type is a primitive (int, long, boolean, etc.). Records cannot store null for primitives and the canonical constructor cannot accept null, so Gson aborts with a JsonParseException including the field name and the reader path.","triggerScenarios":"Triggered in BoundField.readIntoArray (ReflectiveTypeAdapterFactory.java:261) when typeAdapter.read(reader) returns null but isPrimitive is true. The JSON document explicitly has \"field\": null (or omits/produces null via a custom adapter) for a primitive record component such as `int id`.","commonSituations":"API responses that emit null for numeric/boolean fields when data is missing; nullable database columns mapped to primitive record components; upstream services that conditionally include fields; strict-mode configs where clients send `\"count\": null`.","solutions":["Change the record component from primitive to its boxed wrapper (int -> Integer, boolean -> Boolean) so null is acceptable.","Pre-process or sanitize the JSON to replace nulls with defaults before deserialization.","Register a custom TypeAdapter that maps null to a default primitive value (e.g., 0).","Negotiate with the data source to always emit a concrete value for the field."],"exampleFix":"// before\npublic record User(int id, String name) {}\ngson.fromJson(\"{\\\"id\\\":null,\\\"name\\\":\\\"A\\\"}\", User.class); // throws\n\n// after\npublic record User(Integer id, String name) {}","handlingStrategy":"validation","validationCode":"// Reject null values for primitive record components before deserialization\nJsonObject o = JsonParser.parseString(json).getAsJsonObject();\nfor (RecordComponent c : User.class.getRecordComponents()) {\n  if (c.getType().isPrimitive() && o.has(c.getName()) && o.get(c.getName()).isJsonNull()) {\n    throw new IllegalArgumentException(\"null for primitive \" + c.getName());\n  }\n}","typeGuard":"null","tryCatchPattern":"try {\n  gson.fromJson(json, User.class);\n} catch (JsonParseException e) {\n  if (e.getMessage().contains(\"null is not allowed as value for record component\")) {\n    // coerce null -> default or ask caller for clean data\n  } else throw e;\n}","preventionTips":["Use boxed wrappers (Integer/Boolean) for record components that may receive null.","Validate incoming JSON against a schema before handing it to Gson.","Keep a list of primitive record components and check them explicitly for nulls.","Prefer Integer/Long over int/long for external API DTOs."],"tags":["gson","record","deserialization","primitive","null"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}