{"id":"029194c31e706c9a","repo":"square/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/square/retrofit/blob/d0b112dad073b7fe49c953ebc46ff1b424cb1e51/retrofit-converters/gson/src/main/java/retrofit2/converter/gson/GsonConverterFactory.java#L34-L70","documentation":"NullPointerException thrown by GsonConverterFactory.create(Gson) when the supplied Gson instance is null (line 52). The factory stores the Gson instance to obtain TypeAdapters for every request/response conversion, so a null Gson is unusable and the constructor fails fast rather than NPE-ing later during a conversion.","triggerScenarios":"Calling `GsonConverterFactory.create(gson)` where `gson` evaluates to null — e.g. a DI-provided Gson field that was never bound, a lazy initializer that returned null, or explicitly passing null.","commonSituations":"Dependency-injection misconfiguration (Gson bean missing or scoped incorrectly); a `getGson()` helper that can return null when config is absent; module wiring changed during a refactor; test setup forgetting to initialize the Gson field.","solutions":["If you do not need custom Gson configuration, use the no-arg `GsonConverterFactory.create()` which builds a default Gson.","Ensure the Gson instance is non-null before passing: initialize it eagerly or use `Objects.requireNonNull(gson, \"gson\")`.","Fix the DI binding so the Gson bean is always provided (e.g. mark the bean non-optional in your framework)."],"exampleFix":"// before\nGson gson = config.maybeGson(); // returns null when unconfigured\nRetrofit retrofit = new Retrofit.Builder()\n    .addConverterFactory(GsonConverterFactory.create(gson))\n    .build();\n\n// after\nRetrofit retrofit = new Retrofit.Builder()\n    .addConverterFactory(GsonConverterFactory.create()) // default Gson\n    .build();","handlingStrategy":"validation","validationCode":"// Validate before wiring the converter into Retrofit.\nGson gson = Objects.requireNonNull(config.buildGson(), \"gson must not be null\");\nGsonConverterFactory factory = GsonConverterFactory.create(gson);","typeGuard":null,"tryCatchPattern":"try {\n  factory = GsonConverterFactory.create(gson);\n} catch (NullPointerException e) {\n  throw new IllegalStateException(\"Gson was not provided; falling back is disabled\", e);\n}","preventionTips":["Prefer the no-arg GsonConverterFactory.create() unless you have custom Gson config.","Mark the Gson DI bean non-optional so missing bindings fail at startup.","Use Objects.requireNonNull(gson, \"gson\") at the boundary.","Write a unit test that builds your Retrofit instance with the production DI graph."],"tags":["gson","retrofit","converter","null-safety","configuration"],"analyzedSha":"d0b112dad073b7fe49c953ebc46ff1b424cb1e51","analyzedAt":"2026-08-04T19:12:59.096Z","schemaVersion":2}