I'm getting an NullPointerException when im trying to sort my arraylist with "ObjectEpisodes" in it.
The null pointer appears when im trying to sort the ArrayList but then some of the objects dont have a date to sort on. I'm getting those information via JSON and API calls.
What is the best way to handle those null pointers?
My Object implements Comparable:
public Date getDateTime() {
return convertDate(getAirdate());
}
public Date convertDate(String date)
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date inputDate = null;
try {
inputDate = dateFormat.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inputDate;
}
@Override
public int compareTo(SickbeardEpisode another) {
return getDateTime().compareTo(another.getDateTime());
}
Here is were i call Collections.sort(episodes):
private static List<ObjectEpisodes> parseEpisodes(String url) {
List<ObjectEpisode> episodes = new ArrayList<ObjectEpisode>();
String json = download(url);
try {
JSONObject result = new JSONObject(json);
JSONObject resultData = result.getJSONObject("data");
Iterator<String> iter = resultData.keys();
while (iter.hasNext()) {
String key = iter.next();
JSONObject value = resultData.getJSONObject(key);
ObjectEpisode episode = new ObjectEpisode(value);
series.add(serie);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
Collections.sort(episodes);
return series;
}