java日期保持为空(Android)
我正在开发一个简单的日历,将事件和每个事件的时间存储在两个不同的哈希图中,一个用于事件,另一个用于相应事件的时间这是我的代码:
SimpleDateFormat evTime = new SimpleDateFormat("HH:mm");
SimpleDateFormat month = new SimpleDateFormat("MMMM - YYYY");
SimpleDateFormat day = new SimpleDateFormat("MMM dd yyyy");
HashMap<Date, ArrayList<String>> hmap = new HashMap<>();
HashMap<Date, ArrayList<String>> hmap2 = new HashMap<>();
void eventMaker(String d, String ev) {
Date date = null;
Date time = null;
try {
date = day.parse(d);
time = evTime.parse(d);
} catch (ParseException e) {
e.printStackTrace();
}
long epoch = date.getTime();
Event event = new Event(Color.RED,epoch,ev);
compactCalendar.addEvent(event);
if(!hmap.containsKey(date)){
ArrayList<String> eventList = new ArrayList<>();
eventList.add(ev);
hmap.put(date,eventList);
ArrayList<String> eventTimes = new ArrayList<>();
eventTimes.add(time.toString());
hmap2.put(date,eventTimes);
}
else{
ArrayList<String> eventList = hmap.get(date);
eventList.add(ev);
hmap.put(date,eventList);
ArrayList<String> eventTimes = hmap2.get(date);
eventTimes.add(time.toString());
hmap2.put(date,eventTimes);
}
}
问题是变量(时间)保持为空,这会导致崩溃,我想知道为什么
# 1 楼答案
抱歉,伙计们,我没有注意到一个解析异常。我只是误用了解析和格式化方法。。。很抱歉给您带来不便
# 2 楼答案
可以修复nullpointerexception,但需要以不同的方式处理该异常
试试扔吧
然后移除那里的try catch,并在任何你调用该方法的地方执行它
一旦你运行了,检查logcat消息,看看你的日期格式是否错误