{"record":{"id":"6c56630b472c38dd","repo":"dotnet/machinelearning","slug":"throw-new-argumentnullexception-nameof-vocabstream-6c5663","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(vocabStream));","messagePattern":"throw new ArgumentNullException\\(nameof\\(vocabStream\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.Tokenizers/Model/BertTokenizer.cs","lineNumber":699,"sourceCode":"\n        /// <summary>\n        /// Create a new instance of the <see cref=\"BertTokenizer\"/> class asynchronously.\n        /// </summary>\n        /// <param name=\"vocabStream\">The stream containing the vocabulary file.</param>\n        /// <param name=\"options\">The options to use for the Bert tokenizer.</param>\n        /// <param name=\"cancellationToken\">The cancellation token.</param>\n        /// <returns>A task that represents the asynchronous creation of the BertTokenizer.</returns>\n        /// <remarks>\n        /// When creating the tokenizer, ensure that the vocabulary stream is sourced from a trusted provider.\n        /// </remarks>\n        public static async Task<BertTokenizer> CreateAsync(\n                    Stream vocabStream,\n                    BertOptions? options = null,\n                    CancellationToken cancellationToken = default)\n        {\n            if (vocabStream is null)\n            {\n                throw new ArgumentNullException(nameof(vocabStream));\n            }\n\n            (Dictionary<StringSpanOrdinalKey, int> vocab, Dictionary<int, string> vocabReverse) = await LoadVocabAsync(vocabStream, useAsync: true, cancellationToken).ConfigureAwait(false);\n\n            return Create(vocab, vocabReverse, options);\n        }\n\n        /// <summary>\n        /// Create a new instance of the <see cref=\"BertTokenizer\"/> class asynchronously.\n        /// </summary>\n        /// <param name=\"vocabFilePath\">The path to the vocabulary file.</param>\n        /// <param name=\"options\">The options to use for the Bert tokenizer.</param>\n        /// <param name=\"cancellationToken\">The cancellation token.</param>\n        /// <returns>A task that represents the asynchronous creation of the BertTokenizer.</returns>\n        /// <remarks>\n        /// When creating the tokenizer, ensure that the vocabulary file is sourced from a trusted provider.\n        /// </remarks>\n        public static async Task<BertTokenizer> CreateAsync(","sourceCodeStart":681,"sourceCodeEnd":717,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.Tokenizers/Model/BertTokenizer.cs#L681-L717","documentation":"The async BertTokenizer.CreateAsync(Stream vocabStream, ...) throws ArgumentNullException when the supplied vocabStream is null. The stream is validated before LoadVocabAsync reads the vocabulary from it, because there is nothing to load from a null stream.","triggerScenarios":"Calling CreateAsync((Stream)null) — e.g. passing the result of a stream factory that returned null, or forgetting to open the stream before calling.","commonSituations":"Reading vocab from embedded resources where GetManifestResourceStream returned null (wrong resource name); streams opened conditionally and not created in some branches; tests passing null streams.","solutions":["Pass a valid opened Stream containing the vocabulary.","Check the stream-producing call: File.OpenRead, Assembly.GetManifestResourceStream, etc., for null before calling CreateAsync.","Use the vocabFilePath overload CreateAsync(string) which handles opening the file for you."],"exampleFix":"// before\nawait using var stream = GetVocabStream(); // may return null\nvar tokenizer = await BertTokenizer.CreateAsync(stream);\n// after\nawait using var stream = GetVocabStream() ?? throw new InvalidOperationException(\"Vocab stream missing\");\nvar tokenizer = await BertTokenizer.CreateAsync(stream);","handlingStrategy":"type-guard","validationCode":"if (vocabStream is null) throw new InvalidOperationException(\"Vocabulary stream must be opened before CreateAsync\");","typeGuard":"static bool HasVocabStream(Stream? s) => s is not null;","tryCatchPattern":"try { var t = await BertTokenizer.CreateAsync(vocabStream, options, ct); } catch (ArgumentNullException ex) when (ex.ParamName == \"vocabStream\") { /* open the stream and retry once */ }","preventionTips":["Check stream factories (GetManifestResourceStream, File.OpenRead) for null before use.","Prefer the string-path CreateAsync overload to avoid manual stream handling.","Use '?? throw' at the point of stream creation to fail early with context."],"tags":["csharp","argument-null","stream","tokenizer"],"backgroundTag":"null-argument","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}