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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
pattabi23inConnect With a Mentor Author 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
 
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
Never miss a deadline with monday.com

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

 
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
All Courses

From novice to tech pro — start learning today.