[Ljava.lang.Object; cannot be cast to List from a request SQL

769 Views Asked by At

I have a problem for the view of a List with a ForEach in a jsp:

I have this error for the timelineservlet:

Etat HTTP 500 - [Ljava.lang.Object; cannot be cast to fr.gabbler.models.Gabs


type Rapport d''exception

message [Ljava.lang.Object; cannot be cast to fr.gabbler.models.Gabs

description Le serveur a rencontré une erreur interne qui l''a empêché de satisfaire la requête.

exception 
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to fr.gabbler.models.Gabs
    fr.gabbler.servlet.TimelineServlet.doGet(TimelineServlet.java:32)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)

timelineservlet.java

package fr.gabbler.servlet;

import java.io.IOException;
import java.util.List;

import javassist.expr.NewArray;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.jasper.tagplugins.jstl.core.ForEach;

import fr.gabbler.dao.GabDao;
import fr.gabbler.models.Gabs;
import fr.gabbler.models.User;

@WebServlet("/timeline")
public class TimelineServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String user = (String) req.getSession().getAttribute("username");

        GabDao gd = new GabDao();
        List<Gabs> gabs = gd.getListGabs(user);

        //Line which is generating the error
        gabs.get(1).getContent();


        req.setAttribute("gabs", gabs);


        req.getRequestDispatcher("/timeline.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.doPost(req, resp);
    }
}

The function called in the servlet located in gabdao :

public List<Gabs> getListGabs(String username){

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-pu");
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();

    userDao user = new userDao();
    int id = user.idFindByUsername(username);



    Query query = em.createQuery("select DISTINCT Id, content from Gabs as g where g.idUser = (select DISTINCT Id from User as u where u.Id = (select DISTINCT idFollowed from Follow as f where f.idFollower = :id ) )");
    query.setParameter("id", id );


    List<Gabs> gabs = (List<Gabs>) query.getResultList();

     return gabs;

}

In the timeline.jsp i have this code for the List, and without the line in the timelineservlet it does that :

Etat HTTP 500 - java.lang.NumberFormatException: For input string: "content"


<c:forEach items="${gabs}" var="t">
            <div class="container">
                <div id="gabs">
                ${t.content }
                </div>
            </div>
</c:forEach>

There it is, The list have 2 elements, the gabs.size() works

i have searched on the web but i didn't find anything that can help me , that's maybe because the list is typed like List but gabs is typed List so i don"t know

Thank's helping me :)

1

There are 1 best solutions below

0
On BEST ANSWER

The error and the line it's occurring on mean that your Query is returning a List<Object[]> - the line that's throwing the error is the first place you actually try and use a value from the List which you downcast.

You're getting an array from your query because your SELECT statement has two columns (Id, content) per the spec. Assuming that Id is a primary key and content is a field on Gabs, you probably just want

SELECT g from Gabs as g WHERE ...