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]
# 1
Please post your JDK verison.
dubwaia at 2007-7-14 1:25:30 > top of Java-index,Core,Core APIs...
# 2
Mac OS X 10.4.6Java Version: 1.5.0_06Java Home: /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home
mtennesa at 2007-7-14 1:25:30 > top of Java-index,Core,Core APIs...
# 3
Can you reproduce this by setting your system clock? If so, try doing this with assertions turned on. There is an assertion in the code that looks up the defualt time zone that it is not null. The result of that test will help narrow down the source of the problem.
dubwaia at 2007-7-14 1:25:30 > top of Java-index,Core,Core APIs...
# 4
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.
mtennesa at 2007-7-14 1:25:30 > top of Java-index,Core,Core APIs...
# 5

> 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?

dubwaia at 2007-7-14 1:25:30 > top of Java-index,Core,Core APIs...
# 6

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());

}

}

mtennesa at 2007-7-14 1:25:30 > top of Java-index,Core,Core APIs...
# 7

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.

dubwaia at 2007-7-14 1:25:30 > top of Java-index,Core,Core APIs...
# 8

I use IntelliJ Idea as my IDE. I switched from using build 4244 (Irida) to 5321 (Demetra http://intellij.net/eap/) and the problem went away (maybe). I was running the program from within the IDE and I know on MacOS X there's some sort of memory sharing between multiple JVMs

I won't sleep any better at night now that it's gone away, I don't loose any sleep over it anyway.

mtennesa at 2007-7-14 1:25:30 > top of Java-index,Core,Core APIs...