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.

I am getting the class cast Exception as

java.lang.ClassCastException: com.model.Image cannot be cast to java.lang.String

Below is my function to get the Data from an MySQL table created by an Entity Class of JPA

public List<Image> complete(String fileName) 
{
    List<Image> queryResult = new ArrayList<Image>();
    System.out.println("File Name is : "+fileName);
    if (allImages == null) 
    {
        System.out.println("Yeah I am coming here"+allImages);          
        allImages = imageFacade.findAll();
    }       
    allImages.removeAll(servicesWithImage.getImage());
    for (Image image : allImages) 
    {
        if (image.getFileName().toLowerCase().contains(fileName.toLowerCase())) 
        {
            queryResult.add(image);
        }
    }

    return queryResult;
}

I have defined the allImages globally as

private List<Image> allImages;

The above complete method I am calling from an p:autoComplete of Primefaces which is

<p:autoComplete forceSelection="true" 
                minQueryLength="3" 
                value="#{servicesMB.image}" 
                label="#{msgs.image}" 
                required="true" 
                var="image"
                itemLabel="#{image.fileName}" 
                itemValue="#{image}" 
                completeMethod="#{servicesMB.complete}" 
                dropdown="true" />

The findAll method in complete method is shown below :

public List<T> findAll() 
{
    CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    return em.createQuery(cq).getResultList();
 }

If I put the Print Statement in complete method I am getting what all the required data but the data is not shown on p:autoComplete and also I am getting the ClassCastException

I will edit my above complete method with print statements which something looks like

public List<Image> complete(String fileName) 
{
    List<Image> queryResult = new ArrayList<Image>();
    if (allImages == null) 
    {
        System.out.println("I am in If Condition");
        allImages = imageFacade.findAll();
        System.out.println("The Value of allImages : "+allImages);
    }
    System.out.println("Getter function servicesWithImage"+servicesWithImage.getImage());
    allImages.removeAll(servicesWithImage.getImage());
    System.out.println("allImages after removeAll method"+allImages);

    for (Image image : allImages) 
    {   

        if (image.getFileName().toLowerCase().contains(fileName.toLowerCase())) 
        {   
            queryResult.add(image);
        }
    }
    System.out.println("At the End The Value of queryResult"+queryResult);
    return queryResult;
}

My Output(Shown only for method) :

02:58:56,104 INFO  [stdout] (http-localhost-127.0.0.1-8080-5) I am in If Condition
02:58:56,121 INFO  [stdout] (http-localhost-127.0.0.1-8080-5) The Value of allImages :   [services_2.jpg, services_3.jpg]
02:58:56,122 INFO  [stdout] (http-localhost-127.0.0.1-8080-5) Getter function servicesWithImage[]
02:58:56,122 INFO  [stdout] (http-localhost-127.0.0.1-8080-5) allImages after removeAll method[services_2.jpg, services_3.jpg]
02:58:56,123 INFO  [stdout] (http-localhost-127.0.0.1-8080-5) At the End The Value of  queryResult[services_2.jpg, services_3.jpg]
02:58:56,130 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (http-localhost-127.0.0.1-8080-5) 
java.lang.ClassCastException: com.model.Image cannot be cast to java.lang.String

Help...

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Probably is due to the autocomplete.

The autocomplete is like selects, it can't manage objects by itself, needs a converter, and this converter, must be able to convert Image object to a String, and convert this String to the Image again.

You can see a detailed explanation here http://stackoverflow.com/a/8001418/4253629]1

share|improve this answer
    
Took A bit more time to Understand the Solution but was the correct solution Thanks A Lot –  Zaheer Nov 15 '14 at 13:35

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.