Helo All,
I have a problem that I am facing NullPointerException when I want to add an item to the List, as shown below:
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;
}
Please suggest me any solution regarding to the same.
Thanks in advance.
You need to initialize tempMPG, tempPrice and tempRating: