0

大家好,

当我想将一个项目添加到列表时,我遇到了 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;
    }

请向我建议任何有关相同的解决方案。

提前致谢。

4

1 回答 1

3

您需要初始化 tempMPG、tempPrice 和 tempRating:

tempMpg = new ArrayList<SearchResponse>();
tempPrice = new ArrayList<SearchResponse>();
tempRating = new ArrayList<SearchResponse>();
于 2012-07-12T14:54:39.463 回答