使用未分配的局部变量“字典”

Chr*_*ris 3 c# dictionary

尽管我在以下代码中分配了值,但我仍然收到未分配的局部变量“字典”的使用错误:

private static void UpdateJadProperties(Uri jadUri, Uri jarUri, Uri notifierUri)
    {
        Dictionary<String, String> dictionary;

        try
        {
            String[] jadFileContent;

            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader(jadUri.AbsolutePath.ToString()))
            {
                Char[] delimiters = { '\r', '\n' };
                jadFileContent = sr.ReadToEnd().Split(delimiters, System.StringSplitOptions.RemoveEmptyEntries);
            }

            // @@NOTE: Keys contain ": " suffix, values don't!
            dictionary = jadFileContent.ToDictionary(x => x.Substring(0, x.IndexOf(':') + 2), x => x.Substring(x.IndexOf(':') + 2));

        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }

        try
        {
            if (dictionary.ContainsKey("MIDlet-Jar-URL: "))
            {
                // Change the value by Remove follow by Add

            }
        }
        catch (ArgumentNullException ane)
        {

            throw;
        }

    }
Run Code Online (Sandbox Code Playgroud)

错误来自以下行:

if (dictionary.ContainsKey("MIDlet-Jar-URL: "))
Run Code Online (Sandbox Code Playgroud)

请问有人可以帮我吗?TIA

Nic*_*ver 5

您需要在此处明确说明:

Dictionary<String, String> dictionary = null;
Run Code Online (Sandbox Code Playgroud)

当您尝试在第二条语句中使用它时,有可能不会被分配try,例如,如果您在第一次尝试中立即抛出异常,则字典将不会指向任何内容。这不会阻止null引用异常(您必须处理该引用异常),只会使编译器清楚您的意图。