{"record":{"id":"75bf4c317a486289","repo":"dotnet/machinelearning","slug":"throw-new-argumentnullexception-nameof-mergepath","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(mergePath));","messagePattern":"throw new ArgumentNullException\\(nameof\\(mergePath\\)\\);","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.Tokenizers/Model/CodeGenTokenizer.cs","lineNumber":68,"sourceCode":"        /// <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>\n        /// <param name=\"endOfSentenceToken\">The end of sentence token.</param>","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.Tokenizers/Model/CodeGenTokenizer.cs#L50-L86","documentation":"The path-based CodeGenTokenizer constructor also requires mergePath and throws ArgumentNullException from the initializer when it is null. Both files (vocab and merges) are mandatory for this overload because the BPE model needs the merge rules. The null check happens before File.OpenRead so the error names the argument clearly.","triggerScenarios":"new CodeGenTokenizer(\"vocab.json\", null) or mergePath resolved to null from missing configuration; assuming mergePath is optional like it is in BpeOptions.","commonSituations":"Porting code from BpeOptions (where mergesFile is optional) to CodeGenTokenizer; merges file deployment forgotten so the config value stays null.","solutions":["Pass a valid non-null path to the model's merges.txt as the second argument.","Ensure the merges asset is present and its path resolved before construction.","Prefer the Stream-based constructor with explicitly opened streams if you control loading."],"exampleFix":"// before\nvar tokenizer = new CodeGenTokenizer(vocabPath, null);\n// after\nstring mergesPath = Path.Combine(AppContext.BaseDirectory, \"merges.txt\");\nvar tokenizer = new CodeGenTokenizer(vocabPath, File.Exists(mergesPath) ? mergesPath : throw new FileNotFoundException(mergesPath));","handlingStrategy":"validation","validationCode":"if (mergePath is null || !File.Exists(mergePath)) throw new ArgumentException(\"mergePath must be non-null and exist\");","typeGuard":"if (mergePath is string p && p.Length > 0) { /* safe */ }","tryCatchPattern":"try { var tok = new CodeGenTokenizer(vocabPath, mergePath); } catch (ArgumentNullException ex) { logger.LogError(ex, \"Null tokenizer path argument: {Param}\", ex.ParamName); }","preventionTips":["Do not assume optional merges like in BpeOptions; CodeGenTokenizer requires them","Validate both file paths at application startup","Use a factory that resolves all model assets together"],"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-23T08:17:48.524Z"}