Code Beautifier
Cannot map `null` into type `long`

Fix Jackson "Cannot map `null` into type `long`"

A JSON null reached a primitive long, which cannot hold null. Why the generator uses boxed Long for nullable samples, and how to fix an existing class.

Input that triggers it

[{"count": 3}, {"count": null}]
Open JSON to Java on its own page
Draft saved locally.

Local workspace

Named projects in IndexedDB · Local only — never synced to our servers. Worksp

Open manager

Batch workspace

Format multiple files locally in one run.

Style:
Annotations:
json
Javajava

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_PRIMITIVES enabled deliberately, to catch this class of bug rather than silently store zeros.

How to fix it

  1. Paste a sample that includes the null into the generator above. It emits Long rather than long for any field that is null in at least one record, and the same for the other primitives.
  2. 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.
  3. 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.
  4. Consider enabling FAIL_ON_NULL_FOR_PRIMITIVES if it is off: a silent zero in a count or an amount is 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 Long versus Long? and C#'s long versus long? 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 record component has the same constraint: declare it Long, not long, when null is possible.

Related errors