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

时间:2010-06-18 01:57:46

标签: c# dictionary

我收到错误使用未分配的局部变量'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;
        }

    }

错误来自以下行:

if (dictionary.ContainsKey("MIDlet-Jar-URL: "))

任何人都可以帮助我吗,请问? TIA

5 个答案:

答案 0 :(得分:5)

你需要在这里明确:

Dictionary<String, String> dictionary = null;

当您尝试在第二个try语句中使用它时,可能性将不会被分配,例如,如果您在第一次尝试中立即抛出异常,那么字典不指向任何事情。这不会阻止null引用异常(您必须处理它),它只是让编译器明白您的意图。

答案 1 :(得分:1)

编译器不知道你为它分配了什么。对于所有人来说,它知道会抛出一个异常并且永远不会发生任务。

在声明字典时,只需将null指定给字典。

答案 2 :(得分:1)

如果在以下行之前抛出异常:

dictionary = jadFileContent.ToDictionary(x => x.Substring(0, x.IndexOf(':') + 2), x => x.Substring(x.IndexOf(':') + 2));

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);
                throw new Exception();
            }

            // @@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;
        }

    }

答案 3 :(得分:0)

您的try..catch块会创建一个范围。您的字典在第一个try块中初始化,但它可能永远不会到达该代码(例如,如果某些事情之前抛出异常)。所以第二个try块可以访问未初始化的字典。

答案 4 :(得分:0)

问题在于,当您定义它时:Dictionary<String, String> dictionary;您没有初始化它。发生的事情是你在try语句中分配一个值,该语句取决于该语句中的其他内容,可能永远不会进入字典变量的赋值代码。
你可以组合两个try catch块。这样您就不需要初始化它,因为它全部用在同一代码分支中。字典变量在分配之前无法使用。

try
    {
        Dictionary<string,string> dictionary;
        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));


        if(dictionary == null)
        {
           throw new Exception("dictionary is null");
           //or ArgumentNullException since you specified 
           //in the second try catch block.
        }

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

        }

    }
    catch (ArgumentNullException ane)
    {

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