Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

this is a very important question where i got struck...it will be highly appreciable if anyone can answer it!!

Error message:

Feb 19, 2014 6:56:08 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.bdisys.promorphics.domain.Blog
    at org.apache.jsp.Mydetails_jsp._jspService(Mydetails_jsp.java:951)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
    at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
    at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Unknown Source)

 Details of blogs are===[[Ljava.lang.Object;@18fc85, [Ljava.lang.Object;@6a86fe, [Ljava.lang.Object;@1c49309, [Ljava.lang.Object;@9160e3, [Ljava.lang.Object;@6fecea, [Ljava.lang.Object;@2b0e77, [Ljava.lang.Object;@c2c269, [Ljava.lang.Object;@699ec3, [Ljava.lang.Object;@363c6c, [Ljava.lang.Object;@b9141c, [Ljava.lang.Object;@d75341, [Ljava.lang.Object;@9af4bc, [Ljava.lang.Object;@1399537, [Ljava.lang.Object;@173640c, [Ljava.lang.Object;@ad799a, [Ljava.lang.Object;@10aa63a, [Ljava.lang.Object;@274a2b, [Ljava.lang.Object;@1767836, [Ljava.lang.Object;@ce5230, [Ljava.lang.Object;@175fbe3]

DaoImpl Method:

this is my Dao method where i am executing sql query and returning the list of objects to my jsp page for printing it.

public List<Blog> getTopBlogsQuesByDate()throws Exception {
        List<Blog> blogs = new ArrayList<Blog>(0);
        try{
             String sql = "select title , date from (select blog_title as title ,created_date as date from  blog  union select ask_question as title , created_on as date from askquestions ) as aa order by date desc";
             SQLQuery query = getSession().createSQLQuery(sql);
             return query.list();
        }

        catch(Exception e){
            e.printStackTrace();
        }finally{
            closeSession();
        }

        return blogs;
    }

Jsp:

Here am getting data of two tables using sql query but how should i print the list of values in my jsp?????it is showing classcast exception?????it would be highly appreciable if any one can answer it!!thanks in advance.....

<tr>
                    <td style="border: 2px dotted #FFFFFF; border-radius:5px; background-color:#f3ecf0;">
              <% 
                          List<Blog> blog2 =new ArrayList<Blog>();
                          BlogDaoImpl blogDaoImpl1=new BlogDaoImpl();
                          blog2=blogDaoImpl1.getTopBlogsQuesByDate();
                          System.out.println(" Details of blogs are==="+blog2);




                    %>


                         <ul style="text-align: left; left:0px;font-size: 13px; margin-left:-20px; overflow-y: scroll; height:465px;  list-style:url('images/arrows.png');  font-family:Palatino Linotype; color:#2a2a2a; border: 0px solid #d4d4d4" >
                              <% 
           for(int i=0;i<blog2.size();i++){

               Blog blogs1=new Blog();
               blogs1=blog2.get(i);
               System.out.println("blogs size is===="+blog2.size()); 

                blogs1.getId();
                String paths="showblog/"+ blogs1.getBlogTitle()+"/"+blogs1.getId();
         %>
                             <li style="border-bottom : 2px dotted #FFFFFF; margin-bottom: 10px;"><%=blogs1.getBlogText() %></li> 
                   <%} %>
                         </ul>
                    </td>
                </tr>
share|improve this question
 
You have two return statements in your getTopBlogsQuesByDate method, one of which (the first) probably shouldn't be there. You likely want to iterate through your results, creating instances of Blog and adding them to the blogs method variable, then finally returning that at the end of the method. –  Anthony Grist Feb 19 at 13:42
 
i am getting data from two tables 'blogs' and 'askquestions' and returning it to jsp and am getting all the objects as shown in console..but am not able to print them in jsp . that is the exception is occuring..... –  Salman Amaan Feb 19 at 13:45
 
add comment

1 Answer

The way Hibernate handles the return of a subset of fields is to store them in an Object[]. It's the best common upper class to handle N amount of different types of fields (worst case scenario: all of them are different). Since your method returns List<Blog>, the first return statement return query.list() will return a List<Object[]> instead, thus the exception.

You should either select Blog entities directly (if such entity exists) or iterate over every result and store the data collected in each Object[] into a Blog instance, and add it to the return list.

Edit 2

I'm curious as to how did you get so far in such a task without the basic knowledge of list iteration, instance creation and basic list handling... I'm adding pseudocode to further help you.

databaseList = query.list()
iterator = obtain iterator from databaseList 
while the iterator has elements
    obtain element
    create new Blog
    set blog data
    add blog to "blogs" list

The list will be, then, returned by the return blogs; line at the end of your method. Note that Hibernate returns a list of Object[] (array of type Object) where each Object[] represents a returned record. These arrays contain the values of the selected columns in the order they were selected. In your case, they'll have two elements being the blog's title and date in that particular order.

share|improve this answer
 
You should be familiarized with everything by now and if not, I suggest you read the specifications or at least the Javadoc of the methods you use. As to this case, it's fairly simple and quite language agnostic. I'll got for an educational approach for you to understand what you should do, instead of spoon feeding you the code, because if nobody appears next time you could spend another week in block. –  Gamb Feb 19 at 14:06
 
I've added pseudocode and a brief explanation of the structure that Hibernate uses to return the data. –  Gamb Feb 20 at 14:13
 
@SalmanAmaan Do not modify answers to ask more questions or to elaborate on your question/requirements. Thanks. –  TyCobb Feb 21 at 6:15
 
@TyCobb k sure!!! –  Salman Amaan Feb 21 at 6:34
 
@SalmanAmaan The data from the blogs is stored on each Object[] contained on the list. Mapping is not the issue when you're using native queries. Your question is centered on the exception you're having and how to avoid it and the iteration is not the center of my answer, it's a particular hint as to how can you solve the exception issue instead of "making the necessary changes in your code" as you asked. The data is there, if you need further data update your select query. The new fields will be added as additional elements in the array. –  Gamb Feb 24 at 14:27
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.