0

Below is my code in Java, inside a class named DnsTime:

List<DnsTime> list = new ArrayList<DnsTime>();  
String selUrl = "http://www.abc.com";   
String query = "select dns_time,update_time from dns_time where url=?";   
list = session.createSQLQuery(query).setString(0,selUrl).list();  
/* getting dns_time and update_time from each record(row) using 
   for inside another for loop  */

for (int i = 0; i < list.size(); i++) {
    List<DnsTime> innerlist = (List<DnsTime>) list.get(i);
    for (int j = 0; j < innerlist.size(); j++) {
         System.out.println(innerlist.get(j));
    }
}

This is the program code in Java for getting list elements from a list after executing my query. I am using Hibernate. My query is working, but the problem is with getting the list elements: I am getting an exception.

java.lang.ClassCastException: can not cast java.lang.object to java.util.List

How to get the list elements individually from each list row?

1
  • Can you provide the stack trace. Commented Jun 10, 2013 at 7:44

2 Answers 2

4

Your declaration of the initial list is not good; it should be:

List<List<DnsTime>> list = new ArrayList<List<DnsTime>>();

Side note: your should be using foreach loops:

for (List<DnsTime> subList: list)
    for (DnsTime time: subList)
        System.out.println(time);

EDIT: however, I suspect this is not the REAL problem. A few lines later there is:

list = session.createSQLQuery(query).setString(0,selUrl).list();

Now, what does that return?

Sign up to request clarification or add additional context in comments.

5 Comments

still getting the same ClassCastException.Unable to cast java.lang.object to java.util.List
I am using hibernate.Hence session.createSQLQuery()-- return list of records that match with url supplied and list contains records and each record contains dns_time(double value) and update_time(timestamp value)
Well, your problem is probably there: you don't have the correct mapping. Try and debug.
I have the correct results with my query.I ran it in Hibernate run hql query mode.It is giving me the correct database records.But here I am getting classcastexception with the lists and inner lists
I have no doubt that you get the correct results from your database; the doubt I have is that the list you are returned is actually a List<List<DnsName>>.
1

You are trying to cast an object/entry of a list to a list. You can do so only if you have a list of lists, the problem is this line:

List<DnsTime> innerlist=(List<DnsTime>)list.get(i);

list.get(i) will give you an object of DnsTime and not a list of DnsTime ad your list is declared as List<DnsTime> list=new ArrayList<DnsTime>();

Depending on what you are getting from the database you either need to declare the list properly or change the item casting fetched.

1 Comment

Then how can I resolve my problem.Please help me.Returned list contains dns_time of type double and update_time of type timestamp.Plese help me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.