2013-10-13 6 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에서 hashtable에 대해 경고합니다. Null 포인터 액세스 : 변수 productList는이 위치에서만 null 일 수 있습니다.C#에서 Java로 변환

이 문제를 해결하려면 어떻게해야합니까?

+1

Java 코드를 게시해야합니다. – svz

+0

오른쪽. 자바 코드에 문제가있는 경우 해당 코드를 게시하고 문제가 어디에 있는지 설명하십시오. – JHixson

답변

2

productList는 null을 시작하고 때로는 할당되기도합니다.

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

왜 변수를 선언하면 새로운 목록을 만들지 않겠습니까?