2013-10-13 60 views
0
static FreeDutyProductManager() 

    { 

     string fileName = ConfigurationManager.AppSettings["freeDutyProductFile"]; 

     if (!File.Exists(fileName)) 
     { 
      throw new FileNotFoundException("File can't not find:" +fileName); 
     } 

     freeDutyProduct = new Hashtable(); 
     TextReader reader = new StreamReader(File.OpenRead(fileName)); 
     string line = string.Empty; 

     IList<string> productList = null; 
     while ((line = reader.ReadLine()) != null) 
     { 
      if (line.Trim()== string.Empty) 
      { 
       continue; 
      } 
      if (line.Trim().EndsWith(":")) 
      { 
       productList = new List<string>(); 
       freeDutyProduct.Add(line.Replace(":", ""),productList); 
      } 
      else 
      { 
       productList.Add(line.Trim()); 
      } 
     } 
    } 

我想將其轉換爲JAVA,但java會警告我關於散列表空指針訪問:變量productList只能在此位置爲null。將C#轉換爲java

我能做些什麼來解決這個問題?

+1

然後您應該發佈Java代碼。 – svz

+0

對。如果您在使用Java代碼時遇到問題,請發佈該代碼並解釋問題所在。 – JHixson

回答

2

productList從null開始,只有有時被賦值。

 if (line.Trim().EndsWith(":")) 
     { 
      productList = new List<string>(); 
      freeDutyProduct.Add(line.Replace(":", ""),productList); 
     } 
     else 
     { 
      productList.Add(line.Trim()); 
     } 

爲什麼不在新聲明中聲明變量?