{"id":"3c0d695b9f11e58e","repo":"square/retrofit","slug":"mapper-null","errorCode":null,"errorMessage":"mapper == null","messagePattern":"mapper == null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"retrofit-converters/jackson/src/main/java/retrofit2/converter/jackson/JacksonConverterFactory.java","lineNumber":56,"sourceCode":" */\npublic final class JacksonConverterFactory extends Converter.Factory {\n  private static final MediaType DEFAULT_MEDIA_TYPE =\n      MediaType.get(\"application/json; charset=UTF-8\");\n\n  /** Create an instance using a default {@link ObjectMapper} instance for conversion. */\n  public static JacksonConverterFactory create() {\n    return new JacksonConverterFactory(new ObjectMapper(), DEFAULT_MEDIA_TYPE, false);\n  }\n\n  /** Create an instance using {@code mapper} for conversion. */\n  public static JacksonConverterFactory create(ObjectMapper mapper) {\n    return create(mapper, DEFAULT_MEDIA_TYPE);\n  }\n\n  /** Create an instance using {@code mapper} and {@code mediaType} for conversion. */\n  @SuppressWarnings(\"ConstantConditions\") // Guarding public API nullability.\n  public static JacksonConverterFactory create(ObjectMapper mapper, MediaType mediaType) {\n    if (mapper == null) throw new NullPointerException(\"mapper == null\");\n    if (mediaType == null) throw new NullPointerException(\"mediaType == null\");\n    return new JacksonConverterFactory(mapper, mediaType, false);\n  }\n\n  private final ObjectMapper mapper;\n  private final MediaType mediaType;\n  private final boolean streaming;\n\n  private JacksonConverterFactory(ObjectMapper mapper, MediaType mediaType, boolean streaming) {\n    this.mapper = mapper;\n    this.mediaType = mediaType;\n    this.streaming = streaming;\n  }\n\n  /**\n   * Return a new factory which streams serialization of request messages to bytes on the HTTP thread\n   * This is either the calling thread for {@link Call#execute()}, or one of OkHttp's background\n   * threads for {@link Call#enqueue}. Response bytes are always converted to message instances on","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/square/retrofit/blob/d0b112dad073b7fe49c953ebc46ff1b424cb1e51/retrofit-converters/jackson/src/main/java/retrofit2/converter/jackson/JacksonConverterFactory.java#L38-L74","documentation":"NullPointerException thrown by JacksonConverterFactory.create(ObjectMapper, MediaType) when the supplied ObjectMapper is null (line 56). The factory uses the mapper to construct JavaType, ObjectReader, and ObjectWriter for every conversion (lines 84-97), so a null mapper is unusable and the constructor fails fast rather than NPE-ing later during a request.","triggerScenarios":"Calling `JacksonConverterFactory.create(mapper)` or `create(mapper, mediaType)` where `mapper` is null — e.g. a DI-provided ObjectMapper bean that was not bound, a factory method returning null, or explicitly passing null.","commonSituations":"Spring/Guice DI misconfiguration where the ObjectMapper bean is missing or has the wrong qualifier; a builder helper that returns null on config failure; refactor that removed the bean definition; test neglecting to set the mapper.","solutions":["If you do not need custom Jackson configuration, use the no-arg `JacksonConverterFactory.create()` which builds a default ObjectMapper.","Ensure the ObjectMapper is non-null: `Objects.requireNonNull(mapper, \"mapper\")` before passing.","Fix the DI binding so the ObjectMapper bean is always provided with the correct qualifier/scope."],"exampleFix":"// before\nObjectMapper mapper = config.maybeMapper(); // null when unconfigured\nRetrofit retrofit = new Retrofit.Builder()\n    .addConverterFactory(JacksonConverterFactory.create(mapper))\n    .build();\n\n// after\nRetrofit retrofit = new Retrofit.Builder()\n    .addConverterFactory(JacksonConverterFactory.create()) // default ObjectMapper\n    .build();","handlingStrategy":"validation","validationCode":"// Validate before wiring the converter into Retrofit.\nObjectMapper mapper =\n    Objects.requireNonNull(config.buildObjectMapper(), \"mapper must not be null\");\nJacksonConverterFactory factory = JacksonConverterFactory.create(mapper);","typeGuard":null,"tryCatchPattern":"try {\n  factory = JacksonConverterFactory.create(mapper);\n} catch (NullPointerException e) {\n  throw new IllegalStateException(\"ObjectMapper was not provided; fix the DI binding\", e);\n}","preventionTips":["Prefer the no-arg JacksonConverterFactory.create() unless you need a custom ObjectMapper.","Bind the ObjectMapper bean as required (non-optional) in your DI framework.","Use Objects.requireNonNull(mapper, \"mapper\") at the boundary.","Add a unit test that constructs your Retrofit instance through the real DI graph."],"tags":["jackson","retrofit","converter","null-safety","configuration"],"analyzedSha":"d0b112dad073b7fe49c953ebc46ff1b424cb1e51","analyzedAt":"2026-08-04T19:12:59.096Z","schemaVersion":2}