Helo All,
当我想向List添加项目时,我遇到了一个问题,即我面临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;
}
请建议我解决方法。
提前致谢。
答案 0 :(得分:3)
您需要初始化tempMPG,tempPrice和tempRating:
tempMpg = new ArrayList<SearchResponse>();
tempPrice = new ArrayList<SearchResponse>();
tempRating = new ArrayList<SearchResponse>();