{"record":{"id":"a8d82bdd1a2c0cf3","repo":"lysine-dev/retrofit","slug":"context-null","errorCode":null,"errorMessage":"context == null","messagePattern":"context == null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"retrofit-converters/jaxb/src/main/java/retrofit2/converter/jaxb/JaxbConverterFactory.java","lineNumber":45,"sourceCode":"import retrofit2.Converter;\nimport retrofit2.Retrofit;\n\n/**\n * A {@linkplain Converter.Factory converter} which uses JAXB for XML. All validation events are\n * ignored.\n */\npublic final class JaxbConverterFactory extends Converter.Factory {\n  static final MediaType XML = MediaType.get(\"application/xml; charset=utf-8\");\n\n  /** Create an instance using a default {@link JAXBContext} instance for conversion. */\n  public static JaxbConverterFactory create() {\n    return new JaxbConverterFactory(null);\n  }\n\n  /** Create an instance using {@code context} for conversion. */\n  @SuppressWarnings(\"ConstantConditions\") // Guarding public API nullability.\n  public static JaxbConverterFactory create(JAXBContext context) {\n    if (context == null) throw new NullPointerException(\"context == null\");\n    return new JaxbConverterFactory(context);\n  }\n\n  /** If null, a new JAXB context will be created for each type to be converted. */\n  private final @Nullable JAXBContext context;\n\n  private JaxbConverterFactory(@Nullable JAXBContext context) {\n    this.context = context;\n  }\n\n  @Override\n  public @Nullable Converter<?, RequestBody> requestBodyConverter(\n      Type type,\n      Annotation[] parameterAnnotations,\n      Annotation[] methodAnnotations,\n      Retrofit retrofit) {\n    if (type instanceof Class && ((Class<?>) type).isAnnotationPresent(XmlRootElement.class)) {\n      return new JaxbRequestConverter<>(contextForType((Class<?>) type), (Class<?>) type);","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/lysine-dev/retrofit/blob/d0b112dad073b7fe49c953ebc46ff1b424cb1e51/retrofit-converters/jaxb/src/main/java/retrofit2/converter/jaxb/JaxbConverterFactory.java#L27-L63","documentation":"Thrown by JaxbConverterFactory.create(JAXBContext) as an explicit NullPointerException when the supplied JAXBContext is null. The no-arg create() deliberately passes null to the private constructor to signal 'create a fresh context per type', but the public create(JAXBContext) treats null as a programmer error and fails fast so the misconfiguration is reported at the call site, not later during conversion.","triggerScenarios":"Calling JaxbConverterFactory.create((JAXBContext) null) explicitly, or passing a variable that resolved to null (e.g. JAXBContext.newInstance(...) that the caller failed to assign, or a DI-provided context that was never bound).","commonSituations":"A DI provider returning null for JAXBContext because the binding was missing, a JAXB context build that threw and was swallowed leaving the field null, or confusing the no-arg create() (which is allowed to be null internally) with the one-arg overload (which is not).","solutions":["If you have no specific context, use the no-arg JaxbConverterFactory.create() instead of passing null.","If you intend to supply one, build it first: JaxbConverterFactory.create(JAXBContext.newInstance(MyClass.class)).","If using DI, ensure the JAXBContext @Provides/@Bean binding is present and returns non-null.","Catch/propagate any exception from JAXBContext.newInstance so the field is never silently null."],"exampleFix":"// before\nJAXBContext ctx = buildContextOrNull(); // null on failure\nRetrofit retrofit = new Retrofit.Builder()\n    .addConverterFactory(JaxbConverterFactory.create(ctx))\n    .build();\n\n// after — either pass a real context, or use the no-arg overload:\nRetrofit retrofit = new Retrofit.Builder()\n    .addConverterFactory(JaxbConverterFactory.create())\n    .build();","handlingStrategy":"validation","validationCode":"JAXBContext context = resolveJaxbContext();\nJaxbConverterFactory factory;\nif (context == null) {\n  factory = JaxbConverterFactory.create(); // legal: no-arg overload allows per-type context creation\n} else {\n  factory = JaxbConverterFactory.create(context);\n}","typeGuard":"// Kotlin: non-nullable parameter so the compiler rejects a null context.\nfun converterFactory(context: JAXBContext): JaxbConverterFactory =\n    JaxbConverterFactory.create(context)","tryCatchPattern":"JaxbConverterFactory factory;\ntry {\n  factory = JaxbConverterFactory.create(resolveJaxbContext());\n} catch (NullPointerException e) {\n  if (\"context == null\".equals(e.getMessage())) {\n    factory = JaxbConverterFactory.create(); // fall back to per-type context creation\n  } else {\n    throw e;\n  }\n}","preventionTips":["If you do not have a specific JAXBContext, call JaxbConverterFactory.create() (no-arg) instead of passing null.","Propagate exceptions from JAXBContext.newInstance so the reference is never silently null.","In DI, ensure the JAXBContext binding exists and is covered by a wiring test."],"tags":["retrofit","jaxb","null-check","configuration"],"backgroundTag":null,"analyzedSha":"d0b112dad073b7fe49c953ebc46ff1b424cb1e51","analyzedAt":"2026-08-13T23:49:47.955Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}