• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error java util NoSuchElementException

 
khalid ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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 ]
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the value of colCount ?
 
khalid ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
The value of colcount is 3.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49379
62
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
hi,
One of the column is char(1) datatype.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49379
62
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Between, Has your last problem been solved?
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Great!!!.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic