tags:

views:

214

answers:

3

Hi, Im running following query it will display error message. How to solve this error.

        List<Route>routeList=null;
        List<?> companyList = session.createSQLQuery ("select name " +
                                                  "from company "+
                                                  "where company_id= " + companyId).list();                 
        if(companyList.size() <= 0){
            //throw(new AppException(1018,ErrorMessages.getString("INVALID_USER_ID"))); 
        }


        routeList = new ArrayList<Route>(companyList.size());
    Route vgDetails=null;

        for (int i = 0; i < companyList.size(); i++) {

            vgDetails = new Route(); 
            Object[] row = (Object[])companyList.get(i);                                 
            vgDetails.setRouteName ((String)row[0]);                     
            routeList.add(vgDetails);       
        }                   
    session.getTransaction().commit();
        return routeList;
    }

The errror is:

java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
    at com.claystone.server.user.UserListServiceImpl.getParticipantsDestination(UserListServiceImpl.java:902)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:527)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:166)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.doPost(RemoteServiceServlet.java:86)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:205)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
+3  A: 

You can just write:

vgDetails = new Route();                                 
vgDetails.setRouteName ((String)companyList.get(i));

companyList is already a list of strings because you select just one column.

Additionally, as Thilo's answer also suggests, you can cast your result directly to a List<string> instead of using List<?>.

Ronald Wildenberg
A: 

Your companyList contains Strings, not Object[].

You should write

List<String> companyList = (List<String>) session.createSQLQuery (
      "select name from company where company_id=?", companyId).list();


for (String routeName: companyList) {
        vgDetails = new Route();                            
        vgDetails.setRouteName (routeName);                     
        routeList.add(vgDetails);       
    }               
Thilo
That won't solve the problem because it still leaves the cast to `Object[]` that causes the exception.
Ronald Wildenberg
There will be no more cast to Object[]. That would be a compile error now.
Thilo
That's weird. I saw a first version of your answer where only the `List<String>` cast was described, not the new for loop implementation without the cast to `Object[]`... But I don't see that you've edited your answer...
Ronald Wildenberg
@Ronald. That was a very fast edit. But even with only the first line, the cast to Object[] would become impossible, thanks to generics it would have to be removed in order to compile.
Thilo
@Thilo - You're right. Instead of a runtime error it would now have become a compile-time error.
Ronald Wildenberg
A: 

Hibernate is return an array of String (aka a List of type String)

 List<?> companyList = session.createSQLQuery ("select name " +
                                                  "from company "+
                                                  "where company_id= " + companyId).list();   

Because your query returns a String of names from Company table.

What you have to do return a List

 List<String> companyList = (List<String>)session.createSQLQuery ("select name " +
                                                  "from company "+
                                                  "where company_id= " + companyId).list();

Change your error, from this:

 vgDetails = new Route(); 
            Object[] row = (Object[])companyList.get(i);                                 
            vgDetails.setRouteName ((String)row[0]);                     
            routeList.add(vgDetails);

To this:

vgDetails = new Route();                                
vgDetails.setRouteName (companyList.get(i));                     
routeList.add(vgDetails);

as companyList.get(i) returns a String.

The Elite Gentleman