{"record":{"id":"1e4998e107af69b5","repo":"restsharp/RestSharp","slug":"parameter-value-is-null","errorCode":null,"errorMessage":"Parameter value is null","messagePattern":"Parameter value is null","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/RestSharp/Serializers/Xml/XmlRestSerializer.cs","lineNumber":35,"sourceCode":"public class XmlRestSerializer(IXmlSerializer serializer, IXmlDeserializer deserializer) : IRestSerializer {\n    IXmlDeserializer _deserializer = deserializer;\n    IXmlSerializer   _serializer   = serializer;\n\n    public XmlRestSerializer() : this(new DotNetXmlSerializer(), new DotNetXmlDeserializer()) { }\n\n    public ISerializer         Serializer           => _serializer;\n    public IDeserializer       Deserializer         => _deserializer;\n    public string[]            AcceptedContentTypes => ContentType.XmlAccept;\n    public SupportsContentType SupportsContentType  => contentType => contentType.Value.EndsWith(\"xml\", StringComparison.InvariantCultureIgnoreCase);\n\n    public DataFormat DataFormat => DataFormat.Xml;\n\n    public string? Serialize(Parameter parameter) {\n        if (parameter is not XmlParameter xmlParameter)\n            throw new ArgumentException(\"Supplied parameter is not an XML parameter\", nameof(parameter));\n\n        if (parameter.Value == null)\n            throw new ArgumentNullException(nameof(parameter), \"Parameter value is null\");\n\n        var savedNamespace = _serializer.Namespace;\n        _serializer.Namespace = xmlParameter.XmlNamespace ?? savedNamespace;\n\n        var result = _serializer.Serialize(parameter.Value);\n\n        _serializer.Namespace = savedNamespace;\n\n        return result;\n    }\n\n    public XmlRestSerializer WithXmlSerializer(IXmlSerializer xmlSerializer) {\n        _serializer = xmlSerializer;\n        return this;\n    }\n\n    public XmlRestSerializer WithXmlDeserializer(IXmlDeserializer xmlDeserializer) {\n        _deserializer = xmlDeserializer;","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/Serializers/Xml/XmlRestSerializer.cs#L17-L53","documentation":"XmlRestSerializer.Serialize throws ArgumentNullException if the supplied parameter's Value is null. XML serialization of a null object is undefined (System.Xml.Serialization produces no meaningful output), so RestSharp rejects it explicitly rather than emitting an empty document.","triggerScenarios":"Calling XmlRestSerializer.Serialize on an XmlParameter whose Value property is null; using AddXmlBody with a null object that gets wrapped in an XmlParameter with a null value.","commonSituations":"Passing a null domain object to AddXmlBody; a conditional code path that builds an XmlParameter from a variable that can be null; default(T) for a reference type yielding null.","solutions":["Ensure the object passed to XML serialization is non-null before adding it as a body.","Guard AddXmlBody calls with a null check; skip the body or throw a clearer upstream error.","If null bodies are legitimately possible, decide on a sentinel/empty object instead of null."],"exampleFix":"// before\nrequest.AddXmlBody(maybeNullObject); // null -> throws on serialize\n\n// after\nif (maybeNullObject is not null)\n    request.AddXmlBody(maybeNullObject);","handlingStrategy":"validation","validationCode":"if (parameter is XmlParameter xml && xml.Value is null)\n    throw new ArgumentNullException(nameof(parameter), \"XmlParameter value is null\");\nxmlSerializer.Serialize(parameter);","typeGuard":"static bool HasXmlValue(Parameter p) => p is XmlParameter x && x.Value is not null;","tryCatchPattern":null,"preventionTips":["Null-check objects before AddXmlBody.","For optional bodies, skip the body rather than pass null.","Unit-test XML serialization paths against nullable reference types."],"tags":["xml","serialization","null","argument","request-body"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}