【发布时间】:2014-10-18 22:18:46
【问题描述】:
当我尝试使用“ObjectEpisodes”对我的数组列表进行排序时,我收到了 NullPointerException。
当我尝试对 ArrayList 进行排序时出现空指针,但随后某些对象没有要排序的日期。我通过 JSON 和 API 调用获取这些信息。
处理这些空指针的最佳方法是什么?
我的对象实现了 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());
}
这是我调用 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;
}
【问题讨论】:
标签: java json sorting arraylist collections