{"record":{"id":"e66351f0698cb1ff","repo":"apache/maven","slug":"writer-or-outputstream-must-be-non-null-e66351","errorCode":null,"errorMessage":"writer or outputStream must be non null","messagePattern":"writer or outputStream must be non null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultToolchainsXmlFactory.java","lineNumber":83,"sourceCode":"            xml.setAddDefaultEntities(request.isAddDefaultEntities());\n            if (reader != null) {\n                return xml.read(reader, request.isStrict(), source);\n            } else {\n                return xml.read(inputStream, request.isStrict(), source);\n            }\n        } catch (Exception e) {\n            throw new XmlReaderException(\"Unable to read toolchains: \" + getMessage(e), getLocation(e), e);\n        }\n    }\n\n    @Override\n    public void write(XmlWriterRequest<PersistedToolchains> request) throws XmlWriterException {\n        requireNonNull(request, \"request\");\n        PersistedToolchains content = Objects.requireNonNull(request.getContent(), \"content\");\n        OutputStream outputStream = request.getOutputStream();\n        Writer writer = request.getWriter();\n        if (writer == null && outputStream == null) {\n            throw new IllegalArgumentException(\"writer or outputStream must be non null\");\n        }\n        try {\n            MavenToolchainsStaxWriter xmlWriter = new MavenToolchainsStaxWriter();\n            xmlWriter.setAddLocationInformation(false);\n\n            Function<Object, String> formatter = request.getInputLocationFormatter();\n            if (formatter != null) {\n                xmlWriter.setAddLocationInformation(true);\n                Function<InputLocation, String> adapter = formatter::apply;\n                xmlWriter.setStringFormatter(adapter);\n            }\n\n            if (writer != null) {\n                xmlWriter.write(writer, content);\n            } else {\n                xmlWriter.write(outputStream, content);\n            }\n        } catch (Exception e) {","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultToolchainsXmlFactory.java#L65-L101","documentation":"DefaultToolchainsXmlFactory.write accepts only a Writer or an OutputStream; there is no path support. If both are null the call is rejected with IllegalArgumentException before serialization starts.","triggerScenarios":"Building a write request for PersistedToolchains with only .path(...) set (ignored here), or with no output target at all.","commonSituations":"Porting code from the model factory's path-based write; tests that set only the content.","solutions":["Use .writer(...) or .outputStream(...) on the request","For file output, open the stream yourself: .outputStream(Files.newOutputStream(path)) inside try-with-resources","For in-memory output use .writer(new StringWriter())"],"exampleFix":"// before\ntoolchainsXmlFactory.write(XmlWriterRequest.builder()\n    .content(toolchains)\n    .path(toolchainsPath) // no path support on this factory\n    .build()); // throws\n\n// after\ntry (OutputStream os = Files.newOutputStream(toolchainsPath)) {\n    toolchainsXmlFactory.write(XmlWriterRequest.builder()\n        .content(toolchains)\n        .outputStream(os)\n        .build());\n}","handlingStrategy":"validation","validationCode":"XmlWriterRequest<PersistedToolchains> req = XmlWriterRequest.builder()\n    .content(toolchains)\n    .build();\nif (req.getWriter() == null && req.getOutputStream() == null) {\n    throw new IllegalStateException(\"toolchains write request has no writer or outputStream\");\n}\ntoolchainsXmlFactory.write(req);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["No path support here: open the stream yourself inside try-with-resources","Set content and target in one builder expression"],"tags":["maven","toolchains","xml","writer","null-check"],"backgroundTag":"missing-required-argument","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}