NullPointerException in java.util.GregorianCalendar
Shortly after midnight I get the following exception when trying to create a new Calendar. Has anyone encountered this bug?java.lang.NullPointerException
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2000)
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:1970)
at java.util.Calendar.setTimeInMillis(Calendar.java:1066)
at java.util.GregorianCalendar. (GregorianCalendar.java:576)
at java.util.Calendar.createCalendar(Calendar.java:968)
at java.util.Calendar.getInstance(Calendar.java:909)
The problem seems to be that the Calendar.getZone() method returns null.privateint computeFields(int fieldMask,int tzMask){
int zoneOffset = 0;
TimeZone tz = getZone();
if (zoneOffsets ==null){
zoneOffsets =newint[2];
}
if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)){
if (tzinstanceof ZoneInfo){
zoneOffset = ((ZoneInfo)tz).getOffsets(time, zoneOffsets);
}else{
zoneOffset = tz.getOffset(time);// line 2000 NULLPOINTEREXCEPTION HERE
zoneOffsets[0] = tz.getRawOffset();
zoneOffsets[1] = zoneOffset - zoneOffsets[0];
}
}
if (tzMask != 0){
if (isFieldSet(tzMask, ZONE_OFFSET)){
zoneOffsets[0] = internalGet(ZONE_OFFSET);
}
if (isFieldSet(tzMask, DST_OFFSET)){
zoneOffsets[1] = internalGet(DST_OFFSET);
}
zoneOffset = zoneOffsets[0] + zoneOffsets[1];
}
[2433 byte] By [
mtennesa] at [2007-10-2 22:08:43]

> Hmmm, I ran the program with -esa
> -ea:java.util...
but nothing was ever asserted
> before the program died with the
> NullPointerException. Where is this assertion?
>
> I cannot reproduce this by setting my system clock.
There is an assertion in the TimeZone stuff when getting the default locale. At the very end, it asserts the found time zone is not null.
You can't reproduce it but it happens every day at midnight?Hmmm. I'm starting to think there is a bug in your code. Do you ever call setTimeZone? What happens at midnight that you need a new Calendar? Is the application multithreaded?
The application is multi-threaded. Should it matter why I need a new Calendar? Should I be able to call this method without it failing?
This is the only method in our entire code base that sets the TimeZone.public static String getTextFor(TimePeriod period) {
Calendar start = Calendar.getInstance();
start.setTime(period.getStartDate());
final TimeZone timeZone = period.getPeriodDefinition().getTimeZone();
assert timeZone != null : "Null Time Zone";
start.setTimeZone(timeZone);
Calendar end = Calendar.getInstance();
end.setTime(period.getEndDate());
end.setTimeZone(timeZone);
if ((start.get(Calendar.YEAR) == end.get(Calendar.YEAR)) && (start.get(Calendar.MONTH) == end.get(Calendar.MONTH)) && (start.get(Calendar.DAY_OF_MONTH) == end.get(Calendar.DAY_OF_MONTH)))
{
return new SimpleDateFormat("MM/dd/yy HH:mm").format(period.getStartDate()) + " - " + new SimpleDateFormat("HH:mm").format(period.getEndDate());
} else {
return new SimpleDateFormat("MM/dd/yy HH:mm").format(period.getStartDate()) + " - " + new SimpleDateFormat("MM/dd/yy HH:mm").format(period.getEndDate());
}
}
I don't know what to tell you. From what I can see and what
you have told me, I don't see how you can get this error. I do
know that there have occasionally been bugs in the JVM that
resulted in incorrect reordering of the instructions. It's possible
(if unlikely) that this could be an issue here. But unless you can
reproduce the error at will, I don't know that you'll be able to
determine whether it is the case or not.