开发者

java.lang.ClassCastException: [Ljava.lang.Object; and how to Convert the Class

my DAO is

public class bookRequestStatusDAO {

    public static  List<Book> getbookRequest()
    {

           Session session=null;
           try
           {
                 List booklist=new ArrayList();

            session = HibernateUtil.getSession();
            System.out.println(" Session :: "+session);
            Query query=session.createSQLQuery("{call SP_bookRequestTable()}");
            booklist=query.list();
            System.out.println("in dao getrequest----"+booklist.size());
            if(booklist!=null)
            return booklist;
            else
                return new ArrayList();
           }
           catch(Exception e)
           {
               System.out.println("Exception in BookREqueststatus::"+e);
               return  null;
           }
           finally
           {
               session.close();

           }

    }

my Struts Action Class is

public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
          System.out.println(" exce -----");
        Bookre开发者_如何学运维queststatusForm bookrequeststatusForm=(BookrequeststatusForm)form;
        List<Book> convertlist=new ArrayList<Book>();
           convertlist=bookRequestStatusDAO.getbookRequest();


         System.out.println("list in Action::" +convertlist.size() );
          request.setAttribute("bookDeatils",convertlist );
        System.out.print("request in Action::"+request.getAttribute("bookDeatils"));
        return mapping.findForward(Constants.ACTION_FORWARD_SUCCESS);

    }
}

and in my jsp

<link rel="stylesheet" href="/REC_BookBank/css/bookRequestStatus.css">
<% 
   List<Book>list=new ArrayList<Book>();
    if(request.getAttribute("bookDeatils")!=null)
    {

        list = (ArrayList)request.getAttribute("bookDeatils");
            System.out.println("inside loop List --- "+list.size());
            for(Book book : list)
                {
                out.println("<tr><td>"+book.getId()+"</td></tr>");
            }
               }
    System.out.println(" List --- "+list.size());
%>

my Book.java

import java.io.Serializable;

/**
 *
 * @author Administrator
 */
public class Book implements Serializable {
 private String id;
 private String subject;
 private String title;
 private String author;
 private String publication;
 private String edition;
 private String status;
 private String mode;

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getEdition() {
        return edition;
    }

    public void setEdition(String edition) {
        this.edition = edition;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getPublication() {
        return publication;
    }

    public void setPublication(String publication) {
        this.publication = publication;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}

in tomcat Consul

 exce -----
 Session :: SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[]])
Hibernate: {call SP_bookRequestTable()}
in dao getrequest----1
list in Action::1
request in Action::[[Ljava.lang.Object;@14ef239]inside loop List --- 1

Exception HTTP Status 500 -


type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception in

org.apache.jasper.JasperException: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.rec.bookbank.persitencejavafile.Book
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
    org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1085)
    org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:263)
    org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:398)
    org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:318)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:241)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)


root cause 

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.rec.bookbank.persitencejavafile.Book
    org.apache.jsp.jsp.bookRequestStatus_jsp._jspService(bookRequestStatus_jsp.java:82)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
    org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1085)
    org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:263)
    org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:398)
    org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:318)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:241)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.

I need to print the value here, but it showing the Exception ClassCastException.


the reason why you are getting the exception is because the call to the stored procedure is returning an array of values which is not directly convertable to a class.. you can do one of the 2 things to get a list of books instead of list of object[].

  • you can map the procedure in your .hbm.xml file & call it. (example)
  • you can use a RowMapper class instance to map the rows. (this is something available in spring).

personally I'd prefer mapping the procedure, its a lot more elegant.

example to iterate :

// can be used in DAO to convert the object array into List of Objects
    public List<Book> getList(List<Object[]> bookResult){
    List<Book> books = new ArrayList<Book>();
    for(Object[] array:bookResult){
    Book book= new Book();
    book.setTitle(arr[0]); // you need to set the value of each pojo field here.
    books.add(book);
    }
    return books;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜