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?

user2365917
  • 1,095
  • 5
  • 15
  • 21

2 Answers2

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?

fge
  • 119,121
  • 33
  • 254
  • 329
  • still getting the same ClassCastException.Unable to cast java.lang.object to java.util.List – user2365917 Jun 10 '13 at 10:23
  • 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) – user2365917 Jun 10 '13 at 10:58
  • Well, your problem is probably there: you don't have the correct mapping. Try and debug. – fge Jun 10 '13 at 11:18
  • 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 – user2365917 Jun 11 '13 at 05:59
  • 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>`. – fge Jun 11 '13 at 06:06
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.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 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 – user2365917 Jun 10 '13 at 10:45