wood burning stoves 2.0*
The moose likes Java in General and the fly likes  Problems with java.util.NoSuchElementException: while using enumeration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark " Problems with java.util.NoSuchElementException: while using enumeration" Watch " Problems with java.util.NoSuchElementException: while using enumeration" New topic
Author

Problems with java.util.NoSuchElementException: while using enumeration

Robinson Francis
Greenhorn

Joined: Aug 16, 2010
Posts: 22
Dear all,

I have a problem. With this code:




This is the output I have received:

0class com.portal.pcm.FList
java.util.Hashtable$Enumerator@333128
1class com.portal.pcm.FList
java.util.NoSuchElementException: Vector Enumeration
at java.util.Vector$1.nextElement(Vector.java:305)
at com.tm.iCarePrime.eai.adapter.mcmb.MCMBSimplyForFun.main(MCMBSimplyForFun.java:127)

I don't know why this happening can you please point out to me. Thank you.


John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
A guess - you try to call values.nextElement() twice in line #3 and lne #5. Can you try to make a single values.nextElement() and save it in a variable and use that?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You call nextElement() twice inside the loop. Use a variable to store the results instead:
Also, please KeepItDown - not only in posts but also in subject lines.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Robinson Francis
Greenhorn

Joined: Aug 16, 2010
Posts: 22
Thank you very much to both John Jai and Rob Spoor.

I followed your advice and it ran without problems. Right now, I want to convert from the while loop to for each loop to loop through values. The problem now is that values is of type Enumeration.
Vaues was earlier initialized as below:




With services being a custom reference type for the system I am trying to interface with. The services reference type has getValueEnumerator() method which returns a Enumeration of values. So I am tied to Enumeration. Is there a way to use for each loop.

Thank you.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You can write a wrapper class that implements Iterator and delegates its methods to a backing Enumeration (with remove() throwing an UnsupportedOperationException). However, doing so just to iterate in a slightly different way is rather pointless.

If you want to get rid of the while loop, you can turn it into a for loop already:
 
wood burning stoves
 
subject: Problems with java.util.NoSuchElementException: while using enumeration
 
Similar Threads
hast table and unlimited looping
Regarding the methods in the ServletConfig Interface
HttpServletRequest.getHeaders(aString)
sort an array
NoSuchElementException