2010-06-18 47 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