lysine-dev/retrofit · error · NullPointerException
context == null
Error message
context == null
What it means
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.
Source
Thrown at retrofit-converters/jaxb/src/main/java/retrofit2/converter/jaxb/JaxbConverterFactory.java:45
import retrofit2.Converter;
import retrofit2.Retrofit;
/**
* A {@linkplain Converter.Factory converter} which uses JAXB for XML. All validation events are
* ignored.
*/
public final class JaxbConverterFactory extends Converter.Factory {
static final MediaType XML = MediaType.get("application/xml; charset=utf-8");
/** Create an instance using a default {@link JAXBContext} instance for conversion. */
public static JaxbConverterFactory create() {
return new JaxbConverterFactory(null);
}
/** Create an instance using {@code context} for conversion. */
@SuppressWarnings("ConstantConditions") // Guarding public API nullability.
public static JaxbConverterFactory create(JAXBContext context) {
if (context == null) throw new NullPointerException("context == null");
return new JaxbConverterFactory(context);
}
/** If null, a new JAXB context will be created for each type to be converted. */
private final @Nullable JAXBContext context;
private JaxbConverterFactory(@Nullable JAXBContext context) {
this.context = context;
}
@Override
public @Nullable Converter<?, RequestBody> requestBodyConverter(
Type type,
Annotation[] parameterAnnotations,
Annotation[] methodAnnotations,
Retrofit retrofit) {
if (type instanceof Class && ((Class<?>) type).isAnnotationPresent(XmlRootElement.class)) {
return new JaxbRequestConverter<>(contextForType((Class<?>) type), (Class<?>) type);View on GitHub (pinned to d0b112dad0)
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.
Example fix
// before
JAXBContext ctx = buildContextOrNull(); // null on failure
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(JaxbConverterFactory.create(ctx))
.build();
// after — either pass a real context, or use the no-arg overload:
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(JaxbConverterFactory.create())
.build(); Defensive patterns
Strategy: validation
Validate before calling
JAXBContext context = resolveJaxbContext();
JaxbConverterFactory factory;
if (context == null) {
factory = JaxbConverterFactory.create(); // legal: no-arg overload allows per-type context creation
} else {
factory = JaxbConverterFactory.create(context);
} Type guard
// Kotlin: non-nullable parameter so the compiler rejects a null context.
fun converterFactory(context: JAXBContext): JaxbConverterFactory =
JaxbConverterFactory.create(context) Try / catch
JaxbConverterFactory factory;
try {
factory = JaxbConverterFactory.create(resolveJaxbContext());
} catch (NullPointerException e) {
if ("context == null".equals(e.getMessage())) {
factory = JaxbConverterFactory.create(); // fall back to per-type context creation
} else {
throw e;
}
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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).
Related errors
- gson == null
- mapper == null
- Response must be parameterized as Response<Foo> or Response<
- Result must be parameterized as Result<Foo> or Result<? exte
- Future return type must be parameterized as Future<Foo> or F
AI-assisted analysis of lysine-dev/retrofit@d0b112dad0 (2026-08-13).
Data as JSON: /api/errors/a8d82bdd1a2c0cf3.
Report an issue: GitHub.