2010-06-18 48 views
3

的我得到的未分配的局部变量“字典”尽管我在下面的代码分配值的误差用途:使用未分配的局部变量“字典”

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

回答

4

你需要明确在这里:

Dictionary<String, String> dictionary = null; 

还有的可能性当您尝试,如果你马上抛出一个异常,使用它在你的第二个try语句,例如它不会被分配在第一次尝试中,字典不会指向任何东西。这不会阻止引用异常(您将必须处理该异常),它只是让您的意图清楚的向编译器。

+0

谢谢尼克,傻我! – Chris 2010-06-18 03:43:15

1

编译器不知道你给它分配了什么东西。对于所有它知道一个异常将被抛出,分配将永远不会发生。

只要在声明它时将null分配给字典。

+0

谢谢。 10年前,当我学习Java时,在编程类中没有注意的后果,我认为try..catch在C#中或多或少是相同的。 – Chris 2010-06-18 03:45:42

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

    } 
0

你try..catch块创建一个范围。你的字典在第一个try块中初始化,但它可能永远不会到达那个代码(例如,如果之前引发了异常)。所以第二个try块可能会访问未初始化的字典。

0

你的问题是,当你定义它时:Dictionary<String, String> dictionary;你没有初始化它。发生什么是你正在try语句中赋值,这取决于该语句中发生了什么,可能永远不会将它赋值给dictionary变量的赋值代码。
您可以组合两个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); 
    } 
+0

谢谢,与我上面的评论相同。 – Chris 2010-06-18 03:46:10