java.lang.Object无法强制转换为java.lang.Class

时间:2017-07-29 02:47:25

标签: java

我想从数据库中获取记录,但收到错误:[Ljava.lang.Object;无法转换为beans.Book

DAO类具有以下代码:

List<Book> studentList=new BooksDAO().searchBook(cmbBookType.getSelectedItem().toString());
        Iterator<Book> it = studentList.iterator();

        while(it.hasNext()){
            System.out.println("Inside While_btnSearch ");
            System.out.println(it.next());
            Book book = (Book)it.next();
            System.out.println("Iterator converted to book ");
            for(int i=0;i<studentList.size();i++){
                    bookTable.setValueAt(book.getCode(), i, 0);
                    bookTable.setValueAt(book.getName(), i, 1); 
                    bookTable.setValueAt(book.getAuthor(), i, 2);
                    bookTable.setValueAt(book.getPublisher(), i, 3);
                    bookTable.setValueAt(book.getIsbn(), i, 4);
            }   

,错误日志消息在这里:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to beans.Book
at lms.SearchBook.btnSearchActionPerformed(SearchBook.java:286)
at lms.SearchBook.access$200(SearchBook.java:19)
at lms.SearchBook$4.actionPerformed(SearchBook.java:126)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)

searchBook()如下---

public List<Book> searchBook(String b_type){
    Query qr;
    Session session=SessionFact.getSessionFact().openSession();   
    qr=session.createQuery("select b.code,b.name,b.author,b.publisher,b.isbn from Book b where b.type=:bookType");
    qr.setParameter("bookType", b_type);
    System.out.println("Book Search Completed ");
    List<Book> booklist=qr.list();
    session.close();
    return booklist;

}

请帮助我..

1 个答案:

答案 0 :(得分:0)

这意味着您试图以图书形式投放的对象不是图书实例,在投票确认之前尝试调试或检查是instanceof。