{"record":{"id":"98e3580f611a0549","repo":"dotnet/machinelearning","slug":"throw-new-argumentnullexception-nameof-vocabfilepa","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(vocabFilePath));","messagePattern":"throw new ArgumentNullException\\(nameof\\(vocabFilePath\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.Tokenizers/Model/BertTokenizer.cs","lineNumber":665,"sourceCode":"            }\n\n            return OperationStatus.Done;\n        }\n\n        /// <summary>\n        /// Create a new instance of the <see cref=\"BertTokenizer\"/> class.\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        /// <returns>A new instance of the <see cref=\"BertTokenizer\"/> class.</returns>\n        /// <remarks>\n        /// When creating the tokenizer, ensure that the vocabulary file is sourced from a trusted provider.\n        /// </remarks>\n        public static BertTokenizer Create(\n                    string vocabFilePath,\n                    BertOptions? options = null) =>\n            Create(\n                string.IsNullOrEmpty(vocabFilePath) ? throw new ArgumentNullException(nameof(vocabFilePath)) : File.OpenRead(vocabFilePath),\n                options, disposeStream: true);\n\n        /// <summary>\n        /// Create a new instance of the <see cref=\"BertTokenizer\"/> class.\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        /// <returns>A new instance of the <see cref=\"BertTokenizer\"/> class.</returns>\n        /// <remarks>\n        /// When creating the tokenizer, ensure that the vocabulary stream is sourced from a trusted provider.\n        /// </remarks>\n        public static BertTokenizer Create(\n                    Stream vocabStream,\n                    BertOptions? options = null) =>\n            Create(vocabStream, options, disposeStream: false);\n\n        /// <summary>\n        /// Create a new instance of the <see cref=\"BertTokenizer\"/> class asynchronously.","sourceCodeStart":647,"sourceCodeEnd":683,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.Tokenizers/Model/BertTokenizer.cs#L647-L683","documentation":"BertTokenizer.Create(string vocabFilePath, ...) throws ArgumentNullException when vocabFilePath is null or an empty string. The string is validated inline before opening the file, because File.OpenRead on a null/empty path would otherwise throw a less-clear exception. The path must point to a readable BERT vocabulary file from a trusted source.","triggerScenarios":"BertTokenizer.Create(null) or BertTokenizer.Create(\"\") — typically a config value for the vocab path that is unset, or a settings object whose path property defaulted to empty.","commonSituations":"Missing appsettings/config keys for model assets; deployment where the vocab file path env var is not set; typos in configuration property names leaving the path empty.","solutions":["Supply a valid, non-empty path to the vocab.txt file.","Validate the path before calling: if (string.IsNullOrEmpty(path)) fail fast with a clear message.","Fix the configuration source so the vocab path is populated (env var, config file, etc.).","Check File.Exists(path) first to also catch wrong-path (not just empty) issues."],"exampleFix":"// before\nvar tokenizer = BertTokenizer.Create(config.VocabPath);\n// after\nif (string.IsNullOrEmpty(config.VocabPath))\n    throw new InvalidOperationException(\"VocabPath must be configured\");\nvar tokenizer = BertTokenizer.Create(config.VocabPath);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(vocabFilePath)) throw new InvalidOperationException(\"Bert vocab file path must be configured and non-empty\");\nif (!File.Exists(vocabFilePath)) throw new FileNotFoundException(\"Bert vocab file not found\", vocabFilePath);","typeGuard":"static bool IsValidVocabPath(string? path) => !string.IsNullOrWhiteSpace(path) && File.Exists(path);","tryCatchPattern":"try { var t = BertTokenizer.Create(vocabFilePath, options); } catch (ArgumentNullException ex) when (ex.ParamName == \"vocabFilePath\") { /* surface config error to user */ }","preventionTips":["Validate vocab paths at application startup, not at first use.","Source vocab paths from strongly-typed config with required-field validation.","Only download/load vocabs from trusted providers."],"tags":["csharp","argument-null","configuration","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"}