2012-07-12 62 views
0

直升机所有,当NullPointerException异常将项目添加到列表中

我有我面对NullPointerException异常时,我想将项目添加到列表中,如下图所示的一个问题:

public List<SearchResponse> sortMPGListViewForNA(List<SearchResponse> response) 
    { 
     List<SearchResponse> list = response; 
     List<SearchResponse> tempMPG = null, tempPrice = null, tempRating = null; 
     for(int i=0;i<response.size();i++) 
     { 
      if(response.get(i).years.get(0).mpg.equalsIgnoreCase("n/a")) 
      { 
       tempMPG.add(response.get(i)); 
       list.remove(i); 
      } 
      if(response.get(i).years.get(0).price.equalsIgnoreCase("n/a")) 
      { 
       tempPrice.add(response.get(i)); 
       list.remove(i); 
      } 
      if(response.get(i).years.get(0).rating.equalsIgnoreCase("n/a")) 
      { 
       tempRating.add(response.get(i));//NPE Occurs Here 
       list.remove(i); 
      } 
     } 
     response.addAll(tempMPG); 
     response.addAll(tempPrice); 
     response.addAll(tempRating); 
     return response; 
    } 

请向我推荐任何解决方案。

在此先感谢。

回答

3

需要初始化tempMPG,tempPrice和tempRating:

tempMpg = new ArrayList<SearchResponse>(); 
tempPrice = new ArrayList<SearchResponse>(); 
tempRating = new ArrayList<SearchResponse>();