{"record":{"id":"d052ac610188ae64","repo":"dotnet/machinelearning","slug":"throw-new-argumentnullexception-nameof-vocabularyp","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(vocabularyPath));","messagePattern":"throw new ArgumentNullException\\(nameof\\(vocabularyPath\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.Tokenizers/Model/CodeGenTokenizer.cs","lineNumber":67,"sourceCode":"        /// <param name=\"addPrefixSpace\">Indicate whether to include a leading space before encoding the text.</param>\n        /// <param name=\"addBeginningOfSentence\">Indicate whether to include the beginning of sentence token in the encoding.</param>\n        /// <param name=\"addEndOfSentence\">Indicate whether to include the end of sentence token in the encoding.</param>\n        /// <param name=\"unknownToken\">The unknown token.</param>\n        /// <param name=\"beginningOfSentenceToken\">The beginning of sentence token.</param>\n        /// <param name=\"endOfSentenceToken\">The end of sentence token.</param>\n        internal CodeGenTokenizer(\n                string vocabularyPath,\n                string mergePath,\n                PreTokenizer? preTokenizer = null,\n                Normalizer? normalizer = null,\n                IReadOnlyDictionary<string, int>? specialTokens = null,\n                bool addPrefixSpace = false,\n                bool addBeginningOfSentence = false,\n                bool addEndOfSentence = false,\n                string? unknownToken = DefaultSpecialToken,\n                string? beginningOfSentenceToken = DefaultSpecialToken,\n                string? endOfSentenceToken = DefaultSpecialToken) :\n            this(vocabularyPath is null ? throw new ArgumentNullException(nameof(vocabularyPath)) : File.OpenRead(vocabularyPath),\n                mergePath is null ? throw new ArgumentNullException(nameof(mergePath)) : File.OpenRead(mergePath),\n                preTokenizer, normalizer, specialTokens, addPrefixSpace, addBeginningOfSentence, addEndOfSentence, unknownToken, beginningOfSentenceToken, endOfSentenceToken, disposeStream: true)\n        {\n        }\n\n        /// <summary>\n        /// Construct tokenizer's model object to use with the English Robert model.\n        /// </summary>\n        /// <param name=\"vocabularyStream\">The stream of a JSON file containing the dictionary of string keys and their ids.</param>\n        /// <param name=\"mergeStream\">The stream of a file containing the tokens's pairs list.</param>\n        /// <param name=\"preTokenizer\">The pre-tokenizer to use.</param>\n        /// <param name=\"normalizer\">The normalizer to use.</param>\n        /// <param name=\"specialTokens\">The dictionary mapping special tokens to Ids.</param>\n        /// <param name=\"addPrefixSpace\">Indicate whether to include a leading space before encoding the text.</param>\n        /// <param name=\"addBeginningOfSentence\">Indicate whether to include the beginning of sentence token in the encoding.</param>\n        /// <param name=\"addEndOfSentence\">Indicate whether to include the end of sentence token in the encoding.</param>\n        /// <param name=\"unknownToken\">The unknown token.</param>\n        /// <param name=\"beginningOfSentenceToken\">The beginning of sentence token.</param>","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.Tokenizers/Model/CodeGenTokenizer.cs#L49-L85","documentation":"This CodeGenTokenizer constructor overload takes vocabularyPath and mergePath file paths and throws ArgumentNullException from the constructor initializer when vocabularyPath is null. Unlike BpeOptions, there is no File.Exists check here; null is the only thing rejected before the file is opened. It fails fast before File.OpenRead would throw a different exception.","triggerScenarios":"Calling the path-based CodeGenTokenizer constructor with a null vocabularyPath, e.g. new CodeGenTokenizer(null, \"merges.txt\") or passing an uninitialized config value.","commonSituations":"Config key for the vocab path missing so the bound value is null; caller believed the parameter was optional because many others have defaults.","solutions":["Pass a valid non-null path to the model's vocab.json as the first argument.","Coalesce configuration with a fallback default path before calling the constructor.","Use the Stream-based constructor if you load the vocabulary from an embedded resource."],"exampleFix":"// before\nvar tokenizer = new CodeGenTokenizer(config[\"VocabPath\"], config[\"MergesPath\"]);\n// after\nstring vocabPath = config[\"VocabPath\"] ?? Path.Combine(AppContext.BaseDirectory, \"vocab.json\");\nstring mergesPath = config[\"MergesPath\"] ?? Path.Combine(AppContext.BaseDirectory, \"merges.txt\");\nvar tokenizer = new CodeGenTokenizer(vocabPath, mergesPath);","handlingStrategy":"validation","validationCode":"if (vocabularyPath is null || !File.Exists(vocabularyPath)) throw new ArgumentException(\"vocabularyPath must be non-null and exist\");","typeGuard":"if (vocabularyPath is string p && p.Length > 0) { /* safe */ }","tryCatchPattern":"try { var tok = new CodeGenTokenizer(vocabPath, mergesPath); } catch (ArgumentNullException ex) { logger.LogError(ex, \"Null tokenizer path argument: {Param}\", ex.ParamName); }","preventionTips":["Coalesce config values with ?? throw before passing","Note that unlike BpeOptions, both paths are required here","Centralize tokenizer construction in one factory method"],"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"}