{"record":{"id":"1f3f762128a6fc0f","repo":"lysine-dev/retrofit","slug":"gson-null","errorCode":null,"errorMessage":"gson == null","messagePattern":"gson == null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"retrofit-converters/gson/src/main/java/retrofit2/converter/gson/GsonConverterFactory.java","lineNumber":52,"sourceCode":" * buffers), you must {@linkplain Retrofit.Builder#addConverterFactory(Converter.Factory) add this\n * instance} last to allow the other converters a chance to see their types.\n */\npublic final class GsonConverterFactory extends Converter.Factory {\n  /**\n   * Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and\n   * decoding from JSON (when no charset is specified by a header) will use UTF-8.\n   */\n  public static GsonConverterFactory create() {\n    return create(new Gson());\n  }\n\n  /**\n   * Create an instance using {@code gson} for conversion. Encoding to JSON and decoding from JSON\n   * (when no charset is specified by a header) will use UTF-8.\n   */\n  @SuppressWarnings(\"ConstantConditions\") // Guarding public API nullability.\n  public static GsonConverterFactory create(Gson gson) {\n    if (gson == null) throw new NullPointerException(\"gson == null\");\n    return new GsonConverterFactory(gson, false);\n  }\n\n  private final Gson gson;\n  private final boolean streaming;\n\n  private GsonConverterFactory(Gson gson, boolean streaming) {\n    this.gson = gson;\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\n   * one of OkHttp's background threads.\n   */\n  public GsonConverterFactory withStreaming() {","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/lysine-dev/retrofit/blob/d0b112dad073b7fe49c953ebc46ff1b424cb1e51/retrofit-converters/gson/src/main/java/retrofit2/converter/gson/GsonConverterFactory.java#L34-L70","documentation":"Thrown by GsonConverterFactory.create(Gson) as an explicit NullPointerException when a caller passes a null Gson instance. The factory stores the Gson reference for all subsequent request/response conversions, so a null value would NPE much later during conversion; the factory fails fast at construction to surface the misconfiguration at the call site.","triggerScenarios":"Calling GsonConverterFactory.create(null) directly, or passing a field/parameter that resolves to null (e.g. an uninitialized Gson field, a DI-provided Gson that was never bound, or a lateinit var read before assignment).","commonSituations":"A Dagger/Hilt @Provides method returning null because its dependency was missing, a Gson instance built conditionally and left null on a code path, or unit tests constructing the factory with a mock that returned null.","solutions":["Pass a non-null Gson: use GsonConverterFactory.create() to get a default instance, or GsonConverterFactory.create(new GsonBuilder()....create()).","If using DI, fix the @Provides Gson binding so it never returns null.","Initialize the Gson field before constructing the factory, or mark it final and assign in the constructor.","In tests, pass a real Gson or a properly stubbed non-null mock."],"exampleFix":"// before\nGson gson = maybeConfiguredGson(); // returns null\nRetrofit retrofit = new Retrofit.Builder()\n    .addConverterFactory(GsonConverterFactory.create(gson))\n    .build();\n\n// after\nGson gson = maybeConfiguredGson();\nif (gson == null) gson = new Gson();\nRetrofit retrofit = new Retrofit.Builder()\n    .addConverterFactory(GsonConverterFactory.create(gson))\n    .build();","handlingStrategy":"validation","validationCode":"// Validate before constructing the factory.\nGson gson = resolveGson();\nif (gson == null) {\n  throw new IllegalStateException(\"Gson instance must not be null before building GsonConverterFactory\");\n}\nGsonConverterFactory factory = GsonConverterFactory.create(gson);","typeGuard":"// Kotlin: use a non-nullable parameter so the compiler rejects null at the call site.\nfun converterFactory(gson: Gson): GsonConverterFactory =\n    GsonConverterFactory.create(gson)","tryCatchPattern":"// Construction-time NPE; catch only if a third-party provider may return null.\nGsonConverterFactory factory;\ntry {\n  factory = GsonConverterFactory.create(resolveGson());\n} catch (NullPointerException e) {\n  if (\"gson == null\".equals(e.getMessage())) {\n    factory = GsonConverterFactory.create(); // fall back to default Gson\n  } else {\n    throw e;\n  }\n}","preventionTips":["Prefer GsonConverterFactory.create() (no-arg) unless you need custom Gson configuration.","Make Gson fields final and initialize them in the constructor.","In DI, annotate the Gson provider with non-null semantics and add a binding test."],"tags":["retrofit","gson","null-check","configuration"],"backgroundTag":null,"analyzedSha":"d0b112dad073b7fe49c953ebc46ff1b424cb1e51","analyzedAt":"2026-08-13T23:49:47.955Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}