This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with java.util.NoSuchElementException: while using enumeration

 
Robinson Francis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Robinson Francis
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic