Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2012
    Rep Power

    Default How to tackle this unexpected ClassCastException (PLZ HELP)

    I am facing a very small problem which is driving me mad..

    I am calling a business method 'public List getPsubcategoryList()' from a stateless sessionbean named SlBrowseProducts .. this method used entitymanager instance to retrieve a list of Objects from Database Table mapped to Entity class named Psubcategory. this I did using a namedQuery. The list returned contains elements of type Object, I have to attach a iterator named myIterator to list and cast eachof the's to the type Psubcategory to make the result element useful and run any getters on it. However this casting returned the following error..

    SEVERE: java.lang.ClassCastException: entitybeans.Psubcategory cannot be cast to entitybeans.Psubcategory

    I tried googling extensively to find the cause for it.. to which I came across something called 'ClassLoader Hell' which might be causing it..

    Is there any solution to this problem. Is there any other way around this... I have tried not using Iterator and instead using for loop ... but even then I have to cast.. which again lands me to the same problem..

    Please Help m tight on schedule.

    ====================JSP PAGE==================

    List<Psubcategory> subcategoryList1 = slbp.getProductSubcategories();
    for(int i=0;i<subcategoryList1.size();i++){
    Psubcategory temp = subcategoryList1.get(i);
    } ===============

    public class SLBrowseProducts implements TestInterface {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("INNOVATIVE-INDOORSPU");
    EntityManager em = emf.createEntityManager();

    public List getProductSubcategories(){
    List subcategoryList=em.createNamedQuery("Psubcategory. findAll").getResultList();
    return subcategoryList;

  2. #2
    swapnasangal is offline Member
    Join Date
    Dec 2011
    Rep Power

    Default Re: How to tackle this unexpected ClassCastException (PLZ HELP)


    can you change the getProductSubCategories with the below code:

    public List getProductSubcategories(){
    List<Psubcategory> subcategoryList = entityManager.createNamedQuery ("Psubcategory.findAll", Psubcategory.class).getResultList();
    return subcategoryList;

    Hope it should work, as we are giving the type of object that we need to cast in the createNamedQuery, returns all the objects of that type .
    Last edited by swapnasangal; 02-14-2012 at 11:40 PM.

Similar Threads

  1. ClassCastException
    By MetalR0 in forum New To Java
    Replies: 5
    Last Post: 08-03-2011, 04:22 AM
  2. ClassCastException
    By Umi in forum New To Java
    Replies: 2
    Last Post: 01-14-2010, 12:51 AM
  3. ClassCastException
    By paulsim in forum Java Applets
    Replies: 2
    Last Post: 08-21-2008, 03:14 PM
  4. ClassCastException
    By Ed in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 06:26 AM
  5. ClassCastException
    By Felissa in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 06:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts