0

I am creating a spring project. Here is the code.

public ModelAndView SController(ModelMap model)
{
    P p = new P();
    List<Object[]> myList = (List<Object[]>)this.myservice.getList("abcd");

    for (Object[] lst : myList)
        p.setName(lst[0].toString());

}

public List<Object[]> getList(String name)
{
     return this.sessionFactory.getCurrentSession().createQuery("FROM P WHERE c_name LIKE ")
        .setString(0,  "%" + name + "%").list();
}

I am getting the error on this line

for (Object[] lst : myList)

Error

request processing failed nested exception is java.lang.classcastexception: com.site.MyClass cannot be cast to [Ljava.lang.Object;

Why this error is coming?

EDIT:

SnapShot of debugger Snapshot

5
  • Could you double-check what the list() method returns ? Commented Feb 22, 2016 at 11:06
  • I checked in eclipse debugger. It returns an array of data! Commented Feb 22, 2016 at 11:07
  • debugger snapshot please update if possible.. Thank you Commented Feb 22, 2016 at 11:08
  • provide complete code. "QUERY" means what ? is it hibernate query or simple sql query ? model return by getList must not be Object[]... It must be MyClass as your exception says that.. Commented Feb 22, 2016 at 11:14
  • @VikrantKashyap, added the screenshot! Commented Feb 22, 2016 at 11:27

4 Answers 4

1

Modifying answer after query is provided in your question as below :

public ModelAndView SController(ModelMap model)
{
    P p = new P();
    List<P> myList = (List<P>) this.myservice.getList("abcd");

    for (P lst : myList)
        p.setName(lst.getName());

}

Here we can typecast the list to have more generic type of list. It might give warning which can be suppressed as we know it will always return list of P objects.

public List getList(String name)
{
     return this.sessionFactory.getCurrentSession().createQuery("FROM P WHERE c_name LIKE ")
        .setString(0,  "%" + name + "%").list();
}

this getList method will return list of P objects where c_name like your name input

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

Comments

1

you can use JSONObject instead. It is suitable for array of data.

Comments

1

This is the javadoc of list method:

Return the query results as a List. If the query contains
multiple results per row, the results are returned in an instance
of Object[].

list returns only List, which is not type safe, it can be List<Object[]> or just List<Object>. You need to check if the list contains single object or object array:

List list = this.myservice.getList("abcd");
   for (Object o : list) {
       if(o instanceof Object[]){
          Object[] array = (Object[]) o;
       } else {  
          //object
       }
   }

EDIT

Class cast exception says it cannot convert MyClass to Object[], my guess is hibernate deserializes directly into MyClass instance. Re-implement your code according to this. You can check it by investigating List list = this.myservice.getList("abcd"); I gave above in debug mode.

Comments

1

try this may help you out..Click

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.