• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error java util NoSuchElementException

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,
I am getting error

Getting error java.util.NoSuchElementException
Please find my code as below


[edit]Add code tags and correct indentation. CR[/edit]
[ November 20, 2008: Message edited by: Campbell Ritchie ]
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the value of colCount ?
 
khalid ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,
The value of colcount is 3.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use the code button; I have edited your post so you can see how much better it would look. Please tell us all the details, including which line you get the Exception on.

Are you sure that every element in the rows is a String? If you write rs.getString(i + 1), what if that column was declared as INT or DECIMAL?
 
khalid ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
One of the column is char(1) datatype.

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have to check whether char(1) comes out as a char or a String; I don't know.

There are methods in the ResultSetMetaData interface which get the type of a column. Maybe getColumnType(). Try that on the three columns and see what prints out.
Also go to the Types class and see what the different numbers mean. If you hunt through the Types class you will probably find fields like STRING and INT and LONG; against them you will see a link which reads something like "constant values." If you click that link you can find out what the different numbers are.

I don't know whether that will sort out your problem, but it is worth trying.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure the exception is thrown in this part of the code? NoSuchElementException is usually thrown by collections. Can you print the full stack trace?
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also tried the same code you posted. I did not get any exception. Kindly post the stack trace(or the line where you are getting the exception) to know the exact reason of error.


Thanks
 
khalid ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
Sorry for the late reply.

Now i am getting error java.util.ConcurrentModificationException.






And i calling the class in the JSp like below
[edit]Disable smilies. CR[/edit]
[ November 21, 2008: Message edited by: Campbell Ritchie ]
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Between, Has your last problem been solved?
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

for (Iterator iter = aList.iterator(); iter.hasNext() {
rHist=new RHistory();
element = (String)iter.next();
rHist.setUsername((element == null ? "" : element));
element = (String)iter.next();
rHist.setDesignation((element == null ? "" : element));
aList.add(rHist);

}



You are iterating over and doing add operation on the same list. it is aList in your case
[ November 21, 2008: Message edited by: Patricia Samuel ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get a ConcurrentModificationException because you are modifying a list while you're iterating trough it:

The Java collection classes don't allow modifying the collection while you're iterating through it. You'll have to find some other way to do this - for example, add the elements to a temporary list which you add to aList after the loop, or something similar.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or if the location inside the list doesn't matter, use a ListIterator instead and use its add method.
 
khalid ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi


now while trying to retrieve values i am getting

java.lang.ClassCastException: java.lang.String error
 
khalid ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


now i am getting java.lang.ClassCastException: java.lang.String
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Khalid,

What type of elements are you getting in your aList? Are these of String type? Check it.

Secondly, Can you tell me what purpose will it solve by adding the last element of RHistory in your list. You are adding elements outside the loop.
[ November 21, 2008: Message edited by: Patricia Samuel ]
 
khalid ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




now its working fine.

Thanks a lot for all your expert comments.
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great!!!.
 
reply
    Bookmark Topic Watch Topic
  • New Topic