Issue Details (XML | Word | Printable)

Key: LANG-429
Type: Bug Bug
Status: Closed Closed
Resolution: Duplicate
Priority: Major Major
Assignee: Unassigned
Reporter: Kai Burjack
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Commons Lang

NullPointerException (NPE) in FastDateFormat.getInstance(FastDateFormat.java:211)

Created: 24/Apr/08 01:04 PM   Updated: 17/May/08 05:24 AM
Return to search
Component/s: None
Affects Version/s: 2.2
Fix Version/s: None

Time Tracking:
Not Specified

Environment: MS Windows XP (SP 2), Java 1.5.0 Update 11
Issue Links:
Duplicate
 

Resolution Date: 17/May/08 05:24 AM


 Description  « Hide
Sometimes (sadly this is not reproducible) the following method call results in a NullPointerException (NPE):

Stacktrace:

java.lang.NullPointerException
at org.apache.commons.lang.time.FastDateFormat.getInstance(FastDateFormat.java:211)
at org.apache.commons.lang.time.DateFormatUtils.format(DateFormatUtils.java:258)
at org.apache.commons.lang.time.DateFormatUtils.format(DateFormatUtils.java:232)

I don't know how this is actually possible, since the exception is being raised when accessing a static field 'cInstanceCache' that will be implicitly initialized...

Could this have something to do with class gc?



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Sebb added a comment - 24/Apr/08 01:33 PM
I think it's possible because the cInstanceCache field is not final - see LANG-367.

Updating to version 2.4 should fix the problem.


Sebb added a comment - 24/Apr/08 01:35 PM
This issue appears to be the same

Julien Aymé added a comment - 24/Apr/08 01:52 PM
IMHO, this is rather an example of a data-race issue due to the cInstanceCache field not being final and not being synchronized in <clinit> in FastDateFormat in Lang 2.2.

I suppose that the following scenario could explain your problem:

  • at least two Threads concurrently access to FastDateFormat.getInstance() method, while FastDateFormat is not being loaded by the current classloader:
  • Thread A loads FastDateFormat.class, and creates cInstanceCache.
  • Thread B enters the FastDateFormat.getInstance() method, BUT FastDateFormat.class is in a incoherent state (cInstanceCache is null) because of memory synchronization and/or compiler optimizations
    => this causes the NullPointerException in Thread B
  • Thread A enters in FastDateFormat.getInstance() method and everything is correct.

I suggest you to upgrade to Lang 2.4 in which the cInstanceCache field has been made final, and see if this problem still occurs.

Furthermore, the semantic of final keyword has been strengthen due to JSR-133 , which is part of Java 5.
I encourage you to read the following section: http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#finalWrong
and the Final Fields paragraph in JSR-133: http://www.cs.umd.edu/~pugh/java/memoryModel/jsr133.pdf


Julien Aymé added a comment - 24/Apr/08 01:54 PM
Ugh, my comment came in late , this issue is indeed the same as LANG-37.

Kai Burjack added a comment - 25/Apr/08 09:21 AM
Thanks for your VERY fast answers!
I will give it a try with 2.4

Henri Yandell added a comment - 11/May/08 06:06 AM
Did 2.4 fix things Kai?

Henri Yandell added a comment - 17/May/08 05:24 AM
Closing with the assumption that this fixed things.