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>(Throwable.java:181)
	at java.lang.Exception.<init>(Exception.java:29)
	at java.lang.RuntimeException.<init>(RuntimeException.java:32)
	at java.util.NoSuchElementException.<init>(NoSuchElementException.java:28)
	at java.util.HashMap$HashIterator.nextEntry(HashMap.java:844)
	at java.util.HashMap$KeyIterator.next(HashMap.java:877)
	at java.util.AbstractCollection.toArray(AbstractCollection.java:176)
	at java.util.ArrayList.<init>(ArrayList.java:136)
	at com.pcg.batch.item.writer.PolicyItemWriter.afterStep(PolicyItemWriter.java:46)

Open in new window

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

Thanks


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

Open in new window

0
objectsCommented:
shouldn't happen.
0
pattabi23inAuthor Commented:
Found a similar problem with netbeans IDE where it was commented that it is fixed in 1.6.  

http://netbeans.org/bugzilla/show_bug.cgi?id=145775

where the java bug is
http://bugs.sun.com/view_bug.do?bug_id=6377302

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
0

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.

objectsCommented:
concurrent access can certainly cause problems as a HashSet is not thread safe
0
objectsCommented:
just to clarify you need to ensure there isn't concurrent access to policyList
0
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.
0
objectsCommented:
> I just made changes to get a Synchronized Collection.

where was policyList coming from?
0
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
Java

From novice to tech pro — start learning today.