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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
pattabi23inAuthor 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.