square/retrofit · error · NullPointerException
context == null
Error message
context == null
What it means
NullPointerException thrown by JaxbConverterFactory.create(JAXBContext) when the supplied context is null (line 45). Note this only applies to the explicit-argument factory: the no-arg create() intentionally passes null to the private constructor (line 39) to mean 'create a fresh JAXBContext per type' (see contextForType at line 79). The guard therefore only rejects an explicit null passed by the caller, failing fast rather than NPE-ing later.
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 want default per-type context creation, call the no-arg `JaxbConverterFactory.create()` instead of passing null.
- If you need a shared context, build one explicitly: `JAXBContext.newInstance(MyType.class)` and ensure it is non-null before passing (it throws JAXBException on failure, so catch and handle, do not null-out).
- On JDK 9+, add `--add-modules java.xml.bind` (or ship the jakarta.xml.bind API/impl) so JAXBContext.newInstance succeeds rather than returning null/throwing.
Example fix
// before
JAXBContext ctx = buildContextOrNull(); // null when construction failed
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(JaxbConverterFactory.create(ctx))
.build();
// after — use the no-arg factory for per-type lazy contexts
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(JaxbConverterFactory.create())
.build(); Defensive patterns
Strategy: validation
Validate before calling
// Distinguish the legitimate no-arg factory (lazy per-type context) from an
// explicit shared context, and never pass an explicit null.
JAXBContext ctx = buildSharedContext(); // returns non-null or throws JAXBException
JaxbConverterFactory factory = (ctx != null)
? JaxbConverterFactory.create(ctx)
: JaxbConverterFactory.create(); // default, per-type lazy context Try / catch
try {
factory = (context != null)
? JaxbConverterFactory.create(context)
: JaxbConverterFactory.create();
} catch (NullPointerException e) {
throw new IllegalStateException(
"JAXBContext was null; use create() for the default per-type context", e);
} Prevention
- Call the no-arg JaxbConverterFactory.create() if you want per-type lazy contexts; never pass null explicitly.
- When sharing a context, build it with JAXBContext.newInstance(Type.class) and let JAXBException propagate instead of catching it into null.
- On JDK 9+, ensure java.xml.bind (or jakarta.xml.bind) is on the module path so context creation succeeds.
- Bind any shared JAXBContext bean as non-optional in your DI framework.
When it happens
Trigger: Calling `JaxbConverterFactory.create(context)` where `context` is null. Distinguish from the legitimate `JaxbConverterFactory.create()` no-arg call, which is fine and produces the per-type lazy behavior.
Common situations: DI-provided JAXBContext bean missing or failed to build (e.g. JAXBException during context creation swallowed); a helper returning null when the context could not be constructed; passing null by mistake intending the default; JDK 9+ module access issues causing JAXBContext.newInstance to fail upstream and yielding null in calling code.
Related errors
- gson == null
- mapper == null
- ListenableFuture return type must be parameterized as Listen
- Response must be parameterized as Response<Foo> or Response<
- CompletableFuture return type must be parameterized as Compl
AI-assisted analysis of square/retrofit@d0b112dad0 (2026-08-04).
Data as JSON: /data/errors/f8f512eb3e8c592c.json.
Report an issue: GitHub.