java.lang.String cannot be cast to Ljava.lang.Object

Asked

Viewed 960 times

1

Trying to list items in a ui:repeat that is inside another, and need to use as parameter the id of the ui:repeat item more external. But I can’t do it because the following error is occurring:

java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;

UI:REPEAT

<ui:repeat var="itemRelato" value="#{livroOrdemController.relatos}" 
varStatus="status">

                <p:panel id="panelRelatos">
                    <h:outputLabel value="Data do Relato: "/>
                    <h:outputText value="#{itemRelato.dataOcorrencia}">
                        <f:convertDateTime pattern="dd/MM/yyyy" />
                    </h:outputText>
                    <br />

                    <h:outputLabel value="Tipo do Relato: "/>
                    <h:outputText value="#{itemRelato.tipoRelato.descricao}"/>
                    <br />

                    <h:outputLabel value="Fase da Obra: "/>
                    <h:outputText value="#{itemRelato.faseObraServico.descricao}"/>
                    <br />

                    <h:outputLabel value="Inicio da Fase: "/>
                    <h:outputText value="#{itemRelato.dataIncioFase}">
                        <f:convertDateTime pattern="dd/MM/yyyy" />
                    </h:outputText>
                    <br />

                    <h:outputLabel value="Términio da Fase: "/>
                    <h:outputText value="#{itemRelato.dataTerminioFase}">
                        <f:convertDateTime pattern="dd/MM/yyyy" />
                    </h:outputText>
                    <br />

                    <h:outputLabel value="Descrição: "/><br />
                    <h:outputText value="#{itemRelato.descricao}"/>
                    <br />

                    <b>
                        <h:outputLabel value="Retificações/Complementos: "/>
                    </b>
                    <ui:repeat var="itemRetificacao" value="#{livroOrdemController.recuperarRetificacoes(itemRelato.idRelato)}"
                               varStatus="statusRetificacao">

                            <li>
                                <h:outputText value="#{itemRetificacao.descricao}"/>
                            </li>
                        </ui:repeat>

                    <br />

                    <p:commandButton value="Retificar Relato"
                                     update="#{livroOrdemController.componentes}"
                                     onclick="dialogRetificacao.show();"
                                     process="@this">

                        <f:setPropertyActionListener value="#{itemRelato}" target="#{livroOrdemController.relatoASerRetificado}"/>


                    </p:commandButton>

Controller Listing Method

public List<RetificacaoRelato> recuperarRetificacoes(Long idRelato) {
    List<RetificacaoRelato> retificacoes = new ArrayList<RetificacaoRelato>();
    Map<String, Object> params = new HashMap<String, Object>();
    String queryRetificacoes = "SELECT rr.descricao FROM RetificacaoRelato rr "
            + "WHERE rr.relato_idrelato = :idrelato";
    params.put("idrelato", idRelato);

    List<Object[]> objects = objectDao.listPesqQuery(queryRetificacoes, params);

    for (Object[] o : objects) {
        RetificacaoRelato retificacaoRelatoTemp = new RetificacaoRelato();
        retificacaoRelatoTemp.setDescricao((String) o[0]);
        retificacoes.add(retificacaoRelatoTemp);
    }

    return retificacoes;

ListPesqQuery method

@Override
public List<T> listPesqQuery(String query, Map<String, Object> params) {
    Query q = getEntityManager().createNativeQuery(query);
    for (String chave : params.keySet()) {
        q.setParameter(chave, params.get(chave));
    }
    return q.getResultList();

}
  • 1

    [Ljava.lang.Object; is the class name Object[]. I suspect the function objectDao.listPesqQuery is returning a list of Strings and not a list of Object[]. Could post the code objectDao.listPesqQuery to confirm?

  • I edited the question with the method

  • The method Query.getResultList returns a List faceless. I don’t know how this method works internally, however, as it is a query that returns only one value per record, I believe it can do type casting for String automatically; If you wanted to force a return type, you can use the class TypedQueryimplementing the interface Query.

1 answer

0


substitute

retificacaoRelatoTemp.setDescricao((String) o[0]);

for

`retificacaoRelatoTemp.setDescricao(String.valueOf(o[0]));` 

or

retificacaoRelatoTemp.setDescricao(o[0].toString);
  • Thanks, solved!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.