This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Error java util NoSuchElementException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error java util NoSuchElementException" Watch "Error java util NoSuchElementException" New topic
Author

Error java util NoSuchElementException

khalid ahmed
Greenhorn

Joined: Nov 19, 2008
Posts: 17
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

Joined: Sep 12, 2007
Posts: 300
What is the value of colCount ?
khalid ahmed
Greenhorn

Joined: Nov 19, 2008
Posts: 17
hi ,
The value of colcount is 3.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38040
    
  22
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

Joined: Nov 19, 2008
Posts: 17
hi,
One of the column is char(1) datatype.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38040
    
  22
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
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

Joined: Nov 19, 2008
Posts: 17
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

Joined: Sep 12, 2007
Posts: 300
Between, Has your last problem been solved?
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Or if the location inside the list doesn't matter, use a ListIterator instead and use its add method.
khalid ahmed
Greenhorn

Joined: Nov 19, 2008
Posts: 17
Hi


now while trying to retrieve values i am getting

java.lang.ClassCastException: java.lang.String error
khalid ahmed
Greenhorn

Joined: Nov 19, 2008
Posts: 17


now i am getting java.lang.ClassCastException: java.lang.String
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
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

Joined: Nov 19, 2008
Posts: 17




now its working fine.

Thanks a lot for all your expert comments.
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Great!!!.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error java util NoSuchElementException
 
Similar Threads
Need help in Connecting my MySQL Database to JTable in Eclipse.
Jtable showing last record only
Getting Last Record in Jtable
spring/jsp -exception
ArrayList ensureCapacity