{"record":{"id":"dfc822727226718d","repo":"dotnet/machinelearning","slug":"value-cannot-be-null-parameter-buffer","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'buffer')","messagePattern":"Value cannot be null\\. \\(Parameter 'buffer'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.Data/Utilities/StreamUtils.cs","lineNumber":196,"sourceCode":"        }\n#endif\n\n        /// <summary>Validates arguments provided to reading and writing methods on <see cref=\"Stream\"/>.</summary>\n        /// <param name=\"buffer\">The array \"buffer\" argument passed to the reading or writing method.</param>\n        /// <param name=\"offset\">The integer \"offset\" argument passed to the reading or writing method.</param>\n        /// <param name=\"count\">The integer \"count\" argument passed to the reading or writing method.</param>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"buffer\"/> was null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\">\n        /// <paramref name=\"offset\"/> was outside the bounds of <paramref name=\"buffer\"/>, or\n        /// <paramref name=\"count\"/> was negative, or the range specified by the combination of\n        /// <paramref name=\"offset\"/> and <paramref name=\"count\"/> exceed the length of <paramref name=\"buffer\"/>.\n        /// </exception>\n        [MethodImpl(MethodImplOptions.AggressiveInlining)]\n        private static void ValidateBufferArguments(byte[] buffer, int offset, int count)\n        {\n            if (buffer is null)\n            {\n                throw new ArgumentNullException(nameof(buffer));\n            }\n\n            if (offset < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(offset), \"Offset must be non-negative.\");\n            }\n\n            if ((uint)count > buffer.Length - offset)\n            {\n                throw new ArgumentOutOfRangeException(nameof(count), \"Count must be non-negative and count must not exceed buffer.Length - offset.\");\n            }\n        }\n\n        // No argument checking is done here. It is up to the caller.\n        private static int ReadAtLeastCore(Stream stream, byte[] buffer, int offset, int minimumBytes, bool throwOnEndOfStream)\n        {\n            Debug.Assert(minimumBytes <= buffer.Length);\n            int count = minimumBytes;","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.Data/Utilities/StreamUtils.cs#L178-L214","documentation":"StreamUtils.ValidateBufferArguments guards the byte[] arguments for ReadExactly and related helpers. A null buffer throws ArgumentNullException, mirroring the System.IO.Stream contract, because reading into a null buffer is meaningless.","triggerScenarios":"Calling StreamUtils.ReadExactly(stream, null, offset, count) or another helper that routes through ValidateBufferArguments with a null byte[] buffer.","commonSituations":"A buffer field/property that was never initialized; a method returning null on allocation failure that is passed straight through; refactorings that changed buffer ownership.","solutions":["Ensure the buffer is allocated (new byte[size]) before calling ReadExactly","Add a null check or lazy initialization of the buffer at the call site","Use a factory method that never returns null for buffer creation"],"exampleFix":"// before\nbyte[] buffer = GetBuffer(); // may return null\nStreamUtils.ReadExactly(stream, buffer, 0, len);\n// after\nbyte[] buffer = GetBuffer() ?? new byte[len];\nStreamUtils.ReadExactly(stream, buffer, 0, len);","handlingStrategy":"validation","validationCode":"if (buffer == null) throw new ArgumentNullException(nameof(buffer), \"Allocate buffer before ReadExactly\");\nStreamUtils.ReadExactly(stream, buffer, offset, count);","typeGuard":"static bool IsValidBuffer(byte[] buffer) => buffer != null;","tryCatchPattern":"try { StreamUtils.ReadExactly(stream, buffer, offset, count); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"buffer\")\n{ /* re-initialize buffer */ }","preventionTips":["Always allocate buffers with new byte[size] before stream reads","Avoid methods that can return null buffers; use eager initialization","Enable nullable reference types to catch uninitialized arrays"],"tags":["argument-validation","null-argument","stream"],"backgroundTag":"null-argument","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}