{"record":{"id":"874a5addb5c6b1b9","repo":"apache/maven","slug":"neither-pomfile-nor-modelsource-can-be-null","errorCode":null,"errorMessage":"neither pomFile nor modelSource can be null","messagePattern":"neither pomFile nor modelSource can be null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"compat/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java","lineNumber":583,"sourceCode":"        final DefaultModelProblemCollector collector =\n                new DefaultModelProblemCollector(new DefaultModelBuildingResult());\n        try {\n            return newResult(readModel(null, pomFile, request, collector), collector.getProblems());\n        } catch (ModelBuildingException e) {\n            return error(collector.getProblems());\n        }\n    }\n\n    private Model readModel(\n            ModelSource modelSource, File pomFile, ModelBuildingRequest request, DefaultModelProblemCollector problems)\n            throws ModelBuildingException {\n        Model model;\n\n        if (modelSource == null) {\n            if (pomFile != null) {\n                modelSource = new FileModelSource(pomFile);\n            } else {\n                throw new NullPointerException(\"neither pomFile nor modelSource can be null\");\n            }\n        }\n\n        problems.setSource(modelSource.getLocation());\n        try {\n            boolean strict = request.getValidationLevel() >= ModelBuildingRequest.VALIDATION_LEVEL_MAVEN_2_0;\n            InputSource source = request.isLocationTracking() ? new InputSource() : null;\n\n            Map<String, Object> options = new HashMap<>();\n            options.put(ModelProcessor.IS_STRICT, strict);\n            options.put(ModelProcessor.INPUT_SOURCE, source);\n            options.put(ModelProcessor.SOURCE, modelSource);\n\n            try {\n                model = modelProcessor.read(modelSource.getInputStream(), options);\n            } catch (ModelParseException e) {\n                if (!strict) {\n                    throw e;","sourceCodeStart":565,"sourceCodeEnd":601,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/compat/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java#L565-L601","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set exactly one input on the request: request.setPomFile(pom), request.setModelSource(new FileModelSource(pom)), or new StringModelSource(pomXml) for in-memory models","Null-check the input at the call site before invoking build","If the pom was supposed to exist, check why the earlier resolution returned null (deleted file, wrong path)"],"exampleFix":"// before: nothing to read from\nModelBuildingRequest request = new DefaultModelBuildingRequest();\nrequest.setModel(model);\nModelBuildingResult r = builder.build(request); // NPE: no pomFile, no modelSource\n\n// after: set exactly one input source\nrequest.setPomFile(pom);\n// or: request.setModelSource(new FileModelSource(pom));\n// or: request.setModelSource(new StringModelSource(pomXml));","handlingStrategy":"validation","validationCode":"// Guard the build entry point\nif (request.getPomFile() == null && request.getModelSource() == null) {\n    throw new IllegalArgumentException(\"Set pomFile or modelSource before build\");\n}\nModelBuildingResult result = builder.build(request);","typeGuard":"static boolean hasModelInput(ModelBuildingRequest r) {\n    return r.getPomFile() != null || r.getModelSource() != null;\n}","tryCatchPattern":null,"preventionTips":["Assert exactly one of pomFile/modelSource is set before calling build","Centralize builder invocation in one wrapper that validates inputs","Log which source (file, URL, in-memory) each build reads from"],"tags":["maven","model-building","null-argument","api-misuse"],"backgroundTag":"missing-required-argument","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}