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.

6
  • 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 +=. Commented Aug 2, 2013 at 5:56
  • 1
    That simply means that customerlistDaoimpl.getCustomerList() doesn't return a List<CustomerList>, but a list of something else. If we had the stack trace and the code of this method, we could help. Commented Aug 2, 2013 at 5:59
  • What class object has returned by itr.next()? Is that possible it is null? Commented Aug 2, 2013 at 5:59
  • Maybe offtopic, but I think it will be useful. Consider Google GSON to generate output json. It will lead to more elegant and manageable code in your application. Commented Aug 2, 2013 at 6: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. Commented Aug 3, 2013 at 14:11

4 Answers 4

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(); 
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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 += "]}";

1 Comment

I'm using generics as in List<CustomerList> ??
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.

3 Comments

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<T> is failing to set to CustomerList and I don't see why.
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
createSQLQuery().list() returns an ArrayList of Objects ... not very helpful ... Generics does not contribute then...
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.

Comments

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.