{"record":{"id":"cd7c8923799fe0cf","repo":"dotnet/machinelearning","slug":"throw-new-argumentnullexception-nameof-vocabfile","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(vocabFile));","messagePattern":"throw new ArgumentNullException\\(nameof\\(vocabFile\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.Tokenizers/Model/BpeOptions.cs","lineNumber":41,"sourceCode":"        {\n            if (vocabulary == null)\n            {\n                throw new ArgumentNullException(nameof(vocabulary));\n            }\n\n            Vocabulary = vocabulary;\n        }\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"BpeOptions\"/> class.\n        /// </summary>\n        /// <param name=\"vocabFile\">The JSON file path containing the dictionary of string keys and their ids.</param>\n        /// <param name=\"mergesFile\">The file path containing the tokens's pairs list.</param>\n        public BpeOptions(string vocabFile, string? mergesFile = null)\n        {\n            if (vocabFile is null)\n            {\n                throw new ArgumentNullException(nameof(vocabFile));\n            }\n\n            if (!File.Exists(vocabFile))\n            {\n                throw new ArgumentException($\"Could not find the vocabulary file '{vocabFile}'.\");\n            }\n\n            using Stream vocabStream = File.OpenRead(vocabFile);\n            Dictionary<string, int>? dictionary = JsonSerializer.Deserialize<Dictionary<string, int>>(vocabStream);\n\n            if (dictionary is null)\n            {\n                throw new InvalidOperationException($\"The content of the vocabulary file '{vocabFile}' is not valid.\");\n            }\n\n            Vocabulary = dictionary;\n\n            if (mergesFile is not null)","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.Tokenizers/Model/BpeOptions.cs#L23-L59","documentation":"BpeOptions requires a vocabulary file path to construct the BPE tokenizer model. The constructor throws ArgumentNullException immediately when the vocabFile argument is null, before any file I/O happens. This is a fail-fast guard so callers learn about the missing argument at construction time rather than deeper in tokenization.","triggerScenarios":"Calling new BpeOptions(null) or new BpeOptions(vocabFile: null, mergesFile: \"merges.txt\"); passing a path variable that was never initialized or defaulted to null from configuration.","commonSituations":"Config binding produced a null path because the settings key was missing; a caller passed null intentionally expecting the parameter to be optional (only mergesFile is optional); refactoring changed a non-null default to null.","solutions":["Pass a valid, non-null vocabulary file path as the first constructor argument.","Resolve the path from configuration or a known model directory and assert it is not null before constructing BpeOptions.","If the vocabulary is only available in memory, use the Stream-based BpeOptions overload instead."],"exampleFix":"// before\nvar options = new BpeOptions(config[\"VocabPath\"]!);\n// after\nstring vocabPath = config[\"VocabPath\"] ?? throw new InvalidOperationException(\"VocabPath not configured\");\nvar options = new BpeOptions(vocabPath);","handlingStrategy":"validation","validationCode":"if (vocabPath is null || !File.Exists(vocabPath)) throw new ArgumentException(\"vocabPath must be a non-null path to an existing vocab.json\");","typeGuard":"if (vocabPath is string p && p.Length > 0) { /* safe to construct */ }","tryCatchPattern":"try { var options = new BpeOptions(vocabPath); } catch (ArgumentNullException ex) { logger.LogError(ex, \"Vocab path was null\"); }","preventionTips":["Resolve paths from a single config accessor that throws on missing keys","Never pass config indexer results (string?) directly; coalesce with ?? throw","Use required members or a record to force paths at construction of your own options"],"tags":["csharp","argument-null","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"}