What the error means
Java has two kinds of numeric type. Primitives — long, int, boolean, double — hold a value and nothing else; they cannot be null. Their boxed counterparts — Long, Integer, Boolean, Double — are objects and can. When Jackson meets a JSON null for a field declared as a primitive, it has no way to represent it, and with FAIL_ON_NULL_FOR_PRIMITIVES enabled it throws Cannot map \null` into type `long`. With the feature disabled (Jackson's default), it silently writes 0` instead, which is arguably worse.
In the example, the second record's count is null. A class generated from only the first record declares long count and will fail, or zero, on the second.
Why it happens
- The sample never contained a null, so the generator had no reason to choose the boxed type.
- The producer distinguishes "unknown" from "zero" and sends null for the former — the right thing for it to do, and exactly what a primitive cannot express.
- A database column that allows NULL serialised straight into JSON.
FAIL_ON_NULL_FOR_PRIMITIVESenabled deliberately, to catch this class of bug rather than silently store zeros.
How to fix it
- Paste a sample that includes the null into the generator above. It emits
Longrather thanlongfor any field that is null in at least one record, and the same for the other primitives. - In an existing class, change the field's type to the boxed form:
Long count. Update any arithmetic that assumed a primitive to handle null. - If null is not a meaningful value for this field, fix the producer to omit the key instead. With the key absent, Jackson leaves the primitive at its default and no error occurs — but only do this if "absent" and "zero" genuinely mean the same thing.
- Consider enabling
FAIL_ON_NULL_FOR_PRIMITIVESif it is off: a silent zero in acountor anamountis a data-corruption bug waiting to be found in a report.
The class that handles the example correctly:
public class Root {
public Long count;
}
If it still fails
- Kotlin's
LongversusLong?and C#'slongversuslong?are the same distinction; JSON to Kotlin and JSON to C# apply the same rule when the sample contains a null. - If the null arrives inside a nested object or a list element, the message names the outer property; the boxed type is needed on the inner class.
- A Java
recordcomponent has the same constraint: declare itLong, notlong, when null is possible.
Related errors
UnrecognizedPropertyException: Unrecognized field
Add the field to the class (regenerate from a sample that includes it), annotate the class with @JsonIgnoreProperties(ignoreUnknown = true), or disable FAIL_ON_UNKNOWN_PROPERTIES on the mapper.
Field required
Declare the field with a default (nickname: Optional[str] = None); the generator does this for any key absent from at least one sample.