0

I'm using Spring 3.1.0, Hibernate 4, JDK 7, to a Tomcat 7 and getting a ClassCastException on the itr.next() method. The aaData object does contain the data.

List<CustomerList> aaData = customerlistDaoimpl.getCustomerList();

/*
 * putting data in a JSON form that DataTables recognizes
 */
String data = "{\"sEcho\": 3, \"iTotalRecords\": " + count + ",\"iTotalDisplayRecords\": " + count + ",\"aaData\": [ ";
Iterator<CustomerList> itr = aaData.iterator();
while(itr.hasNext()){
    CustomerList cl = (CustomerList) itr.next();
    data += "[\"" + cl.getName() + "\",\"" + cl.getAddress() + "\",\"" + cl.getZipcode() + "\",\"" + 
    cl.getPhone() + "\",\"" + cl.getCity() + "\",\"" + cl.getCountry() + "\",\"" + cl.getNote() + "\" ] ";
    count++;
}
data += "]}";

My Dao

@SuppressWarnings("unchecked")
@Override
public List<CustomerList> getCustomerList() {
    List<CustomerList> cuList = null;
    Session session = null;

    try{
        session = sessionfactory.openSession();
        session.beginTransaction();         
        cuList = session.createSQLQuery("select * from customer_list").list();      
        session.getTransaction().commit();
    }catch (RuntimeException e){
        System.out.println(e.getMessage());
    }
    finally
    {
        if(session != null){
            session.close();                
        }
    }

    return cuList;
}

And the trace back

SEVERE: Servlet.service() for servlet [sptestjs] in context with path [/SPTestJs] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.sptestjs.implementation.CustomerList] with root cause java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.sptestjs.implementation.CustomerList at com.sptestjs.implementation.controller.HomeController.getCustomerList(HomeController.java:85) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

I did find the call

SQLQuery cuSQLQuery = session.createSQLQuery("select * from customer_list");

returns a SQLQuery instance and its list is of type ArrayList of Object elements where

Query cuQuery = session.createQuery("from customer_list");

returns null.

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
user931501
  • 91
  • 3
  • 12
  • 4
    What's the full exception? (I'd expect it to tell you what the actual value was.) Btw, you should strongly consider using a `StringBuilder` to append the data here, rather than repeatedly using `+=`. – Jon Skeet Aug 02 '13 at 05:56
  • 1
    That simply means that customerlistDaoimpl.getCustomerList() doesn't return a List, but a list of something else. If we had the stack trace and the code of this method, we could help. – JB Nizet Aug 02 '13 at 05:59
  • What class object has returned by `itr.next()`? Is that possible it is `null`? –  Aug 02 '13 at 05:59
  • Maybe offtopic, but I think it will be useful. Consider [Google GSON](https://code.google.com/p/google-gson/) to generate output json. It will lead to more elegant and manageable code in your application. –  Aug 02 '13 at 06:02
  • Arrggh I'm using generics, from what I've seen of GSON it requires tooo much overhead, I've user the Jackson libs and Spring to return JSON to the front end just return a list is all that is required, passing the data back to a ajax call that is. – user931501 Aug 03 '13 at 14:11
  • ass for the += a developer has many options :} – user931501 Aug 03 '13 at 14:12

4 Answers4

2

Getting a ClassCastException from my iterator next method

This means you aaData is not actually a List<CustomerList> You have type erasure somewhere you have changed it type incorrectly. If you look at the ClassCastException carefully it will tell you what the component type really is.

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.sptestjs.implementation.CustomerList

This suggests the type should actually be

List<Object[]> cuList = session.createSQLQuery("select * from customer_list").list(); 
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • The call cuList = session.createSQLQuery("select * from customer_list").list(); in my dao returns an ArrayList of Object elements and the cast has no effect of course. – user931501 Aug 03 '13 at 14:44
0

Use generics .aaData might contain incorrect data

class CustomerlistDaoimpl{

public  List<CustomerList> getCustomerList(){
.....
}
}

    List<CustomerList> aaData = customerlistDaoimpl.getCustomerList();

    String data = "{\"sEcho\": 3, \"iTotalRecords\": " + count + ",\"iTotalDisplayRecords\": " + count + ",\"aaData\": [ ";
    Iterator<CustomerList> itr = aaData.iterator();
    while(itr.hasNext()){
        CustomerList cl = (CustomerList) itr.next();
        data += "[\"" + cl.getName() + "\",\"" + cl.getAddress() + "\",\"" + cl.getZipcode() + "\",\"" + 
        cl.getPhone() + "\",\"" + cl.getCity() + "\",\"" + cl.getCountry() + "\",\"" + cl.getNote() + "\" ] ";
        count++;
    }
    data += "]}";
Rajesh
  • 2,934
  • 8
  • 42
  • 71
0

Check if the fully qualified classname for the CustomerList class you have imported the same as the one which you used while populating the list. I am curious why you needed to cast anyway, given that you are using generics - next() should return the same type as what the Iterator is built for. In other words, Iterator will always return instances of T when itr.next() is invoked.

Neel
  • 2,100
  • 5
  • 24
  • 47
  • I did the cast just to see if it would make a diff because the next() is returning an Object type that's what's strange. The Iterator is failing to set to CustomerList and I don't see why. – user931501 Aug 04 '13 at 19:08
  • The problem is at the createSQLQuery().list() returns an ArrayList of Objects so the next() can't help but return an Object type. I tried to a get(0) on the list – user931501 Aug 04 '13 at 19:41
  • createSQLQuery().list() returns an ArrayList of Objects ... not very helpful ... Generics does not contribute then... – Neel Aug 07 '13 at 23:34
0

Solved:

Multiple errors, the Exception was misleading, I had Qualified my Dao impl with a sessionFactory id that no longer existed, while auto wiring the sessionFactory that did once fixed on to the next bug. fixed them all and the project is up and running.

user931501
  • 91
  • 3
  • 12