Nadk
Nadk

Reputation: 173

[Ljava.lang.Object; cannot be cast to error

I get a list from my query.list(). After that, I want to display the object inside this list but I got this error for this line for(Commerce[] c: this.newsList) {

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.model.Commerce; com.action.CommerceAction.searchCommerces(CommerceAction.java:35)

Here is my code:

My business service

public List<Commerce[]> getCommercesBySearch(String categorie, String lieux) {
    Query query = hibUtil.getSession().createQuery("from Commerce c, Categorie ca,Lieux l "
            + "where c.categorie=ca.idCategorie and c.lieux=l.idLieux and"
            + " ca.libelle='" + categorie + "' and l.ville='" + lieux + "' ");
    List<Commerce[]> tuples = (List<Commerce[]>) query.list();
    return tuples;
}

My action class

private CommerceService service;
private Commerce commerce = new Commerce();
private List<Commerce[]> newsList;
public String searchCommerces() {
    String libelle = ServletActionContext.getRequest().getParameter("libelle");
    String ville = ServletActionContext.getRequest().getParameter("ville");
    this.newsList = service.getCommercesBySearch(libelle,ville);
    for(Commerce[] c: this.newsList){
        System.out.println(c[0].getNom());
    }
    if (this.newsList.size() > 0) {
        return SUCCESS;
    }
    addActionError("Il n'existe aucun commerce de cette catégorie dans cette ville");   
    return ERROR;
    }

Upvotes: 4

Views: 22980

Answers (3)

alias_boubou
alias_boubou

Reputation: 206

Use select c before your from or you will have some trouble with the returned Object[]. If you don't use this, Hibernate will return a list of Object[] containing more than one object into this. For your problem it will be like:

  • Object[0] is an instance of Commerce
  • Object[1] is Categorie
  • Object[3] is Lieux
  • etc.

You will throw an ClassCastException if you try to cast Object[] into Commerce.

Upvotes: 3

Ho&#224;ng Long
Ho&#224;ng Long

Reputation: 10848

I think the problem lies here:

 Query query = hibUtil.getSession().createQuery("from Commerce c, Categorie ca,Lieux l "
        + "where c.categorie=ca.idCategorie and c.lieux=l.idLieux and"
        + " ca.libelle='" + categorie + "' and l.ville='" + lieux + "' ");
List<Commerce[]> tuples = (List<Commerce[]>) query.list();

That's not correct, considering your idea. I believe that your query should return a List<Commerce>, not List<Commerce[]>.

List<Commerce> tuples = (List<Commerce>) query.list();

To make that works, you need to add SELECT c in your query:

SELECT c from Commerce c, Categorie ca,Lieux l...

It will select a list of object Commerce. If you leave the query like originally, it will return a list of Object array (actually they are Commerce[], Categorie[], Lieux[]...). Not to mention you can't cast directly an array in Java, the objects aren't the same type anyway.

Upvotes: 1

durron597
durron597

Reputation: 32323

I am certain that this statement:

List<Commerce[]> tuples = (List<Commerce[]>) query.list();

produces an unchecked type conversion warning. Your code is polluting the heap by doing this unchecked type conversion. query.list() returns a raw List, which will contain Object[]. Here is the relevant Hibernate documentation:

Return the query results as a List. If the query contains multiple results per row, the results are returned in an instance of Object[].

Note that you cannot cast an array to an array of it's sub-type.

There are a couple of ways to fix this problem:

  1. Use List<Object[]> instead of List<Commerce[]>. You can then transform the result of the list() method into a more usable form, preferably within it's own method, before passing it on to the rest of your code. This is the preferable method if you need to select more than just the Commerce object.
  2. Add SELECT c to the beginning of your query, this will allow you to do a safe List<Commerce> cast.

Upvotes: 7

Related Questions