nullpointerexception using calendar.settime

I couldn’t work out why this was giving me a null pointer exception.

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Calendar inputDate;
...
tempVal = attributes.getValue("time");
inputDate.setTime(sdf1.parse(tempVal));

To try to identify the problem I tried different ways including:

SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Calendar inputDate;
...
tempVal = "Mon Mar 14 16:02:37 GMT 2011";
Date d1 = sdf1.parse(tempVal);
inputDate.setTime(d1);

It was always the setTime that caused the exception.

It’s some kind of bug that means when null is set into a calendar item and setTime is the next use of that item then you get the exception (see here).

Happily solution is simple. Do this first.

Calendar inputDate = Calendar.getInstance();

 

This entry was posted in Android. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *