apache/maven · error · NullPointerException

neither pomFile nor modelSource can be null

Error message

neither pomFile nor modelSource can be null

What it means

DefaultModelBuilder.readModel requires the model to come from somewhere: either a ModelSource or a pom File. When both are null there is nothing to read and it throws NullPointerException immediately, before any parsing or validation happens.

Source

Thrown at compat/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java:583

        final DefaultModelProblemCollector collector =
                new DefaultModelProblemCollector(new DefaultModelBuildingResult());
        try {
            return newResult(readModel(null, pomFile, request, collector), collector.getProblems());
        } catch (ModelBuildingException e) {
            return error(collector.getProblems());
        }
    }

    private Model readModel(
            ModelSource modelSource, File pomFile, ModelBuildingRequest request, DefaultModelProblemCollector problems)
            throws ModelBuildingException {
        Model model;

        if (modelSource == null) {
            if (pomFile != null) {
                modelSource = new FileModelSource(pomFile);
            } else {
                throw new NullPointerException("neither pomFile nor modelSource can be null");
            }
        }

        problems.setSource(modelSource.getLocation());
        try {
            boolean strict = request.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0;
            InputSource source = request.isLocationTracking() ? new InputSource() : null;

            Map<String, Object> options = new HashMap<>();
            options.put(ModelProcessor.IS_STRICT, strict);
            options.put(ModelProcessor.INPUT_SOURCE, source);
            options.put(ModelProcessor.SOURCE, modelSource);

            try {
                model = modelProcessor.read(modelSource.getInputStream(), options);
            } catch (ModelParseException e) {
                if (!strict) {
                    throw e;

View on GitHub (pinned to e4093d4e12)

Solutions

  1. Set exactly one input on the request: request.setPomFile(pom), request.setModelSource(new FileModelSource(pom)), or new StringModelSource(pomXml) for in-memory models
  2. Null-check the input at the call site before invoking build
  3. If the pom was supposed to exist, check why the earlier resolution returned null (deleted file, wrong path)

Example fix

// before: nothing to read from
ModelBuildingRequest request = new DefaultModelBuildingRequest();
request.setModel(model);
ModelBuildingResult r = builder.build(request); // NPE: no pomFile, no modelSource

// after: set exactly one input source
request.setPomFile(pom);
// or: request.setModelSource(new FileModelSource(pom));
// or: request.setModelSource(new StringModelSource(pomXml));
Defensive patterns

Strategy: validation

Validate before calling

// Guard the build entry point
if (request.getPomFile() == null && request.getModelSource() == null) {
    throw new IllegalArgumentException("Set pomFile or modelSource before build");
}
ModelBuildingResult result = builder.build(request);

Type guard

static boolean hasModelInput(ModelBuildingRequest r) {
    return r.getPomFile() != null || r.getModelSource() != null;
}

Prevention

When it happens

Trigger: Invoking model building with a null ModelSource and null pomFile, e.g. a ModelBuildingRequest where neither setPomFile nor setModelSource was called, or a null argument leaking through from a caller that resolved the input earlier.

Common situations: Wrappers or tests constructing ModelBuildingRequest without setting the input; copying a build invocation while forgetting the pom argument; a pom file that became null after a failed resolution step.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of apache/maven@e4093d4e12 (2026-08-21). Data as JSON: /api/errors/874a5addb5c6b1b9. Report an issue: GitHub.