NoSuchElementException when creating a new Arraylist using a Collection

The exception trace is given below. The line #46 initializes an ArrayList like this.

            Element element = new Element("RECALLED_POLICY", new ArrayList<Policy>(policyList));

Where policyList is an HashSet and is not empty. Could anyone tell me what scenarios can cause nosuchelementexception during initializing a Collection?

Caused by: java.util.NoSuchElementException
	at java.lang.Throwable.<init>(
	at java.lang.Exception.<init>(
	at java.lang.RuntimeException.<init>(
	at java.util.NoSuchElementException.<init>(
	at java.util.HashMap$HashIterator.nextEntry(
	at java.util.HashMap$
	at java.util.AbstractCollection.toArray(
	at java.util.ArrayList.<init>(
	at com.pcg.batch.item.writer.PolicyItemWriter.afterStep(

Open in new window

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

pattabi23inConnect With a Mentor Author Commented:
Found a similar problem with netbeans IDE where it was commented that it is fixed in 1.6.

where the java bug is

from the stacktrace, I could see that the Problem started at toArray of AbstractList but wonder why it happens in my case as there is no concurrent modification to that collection
pattabi23inAuthor Commented:
After posting I noticed from the trace that the error is thrown when initializing with a HashMap. It is in a different line #( 49 and not 46) which could be empty.
I can change the code to check for empty HashMap - if that is the reason.
Note: It is working fine in my desktop (java 1.6) but the server has 1.5. Is it being handled in 1.6 and not in 1.5?


		Element errored = new Element(BatchConstants.POLICY_CACHE_KEY, new ArrayList<Policy>(erroredPolicy.values()));

Open in new window

shouldn't happen.
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

concurrent access can certainly cause problems as a HashSet is not thread safe
just to clarify you need to ensure there isn't concurrent access to policyList
pattabi23inAuthor Commented:
I am sure that there is no concurrent access to the Map/set. I just made changes to get a Synchronized Collection. Hope this should fix.
> I just made changes to get a Synchronized Collection.

where was policyList coming from?
All Courses

From novice to tech pro — start learning today.