Using the unrecognized local variable 'dictionary'

I got an error. Using the unrecognized local variable dictionary, even though I assigned a value in the following code:

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

    }

Error from line:

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

Can someone help me here, pls? TIA

+3
source share
5 answers

Here you should be explicit:

Dictionary<String, String> dictionary = null;

In this case, it will not be assigned when you try to use it in the second statement try, for example, if you immediately throw an exception in the first attempt, the dictionary will not point to anything. This will not stop the help exception null(you have to handle this), it just makes your intention clear to the compiler.

+4

, - . , , .

null , .

+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;
        }

    }
+1

try..catch . try, (, - ). , try .

0

The problem is that when you defined it: Dictionary<String, String> dictionary;you did not initialize it. What happens is that you assign a value to the try statement, which, depending on what else happens in this expression, can never get into the destination code of the dictionary variable. You can combine two catch try blocks. Thus, you do not need to initialize it, because they are all used in the same code branch. You cannot use a dictionary variable before it is assigned.

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);
    }
0
source

Source: https://habr.com/ru/post/1750609/


All Articles