3 Replies Latest reply on May 28, 2007 5:25 PM by Ignacio de Cordoba

    Without rhyme or reason? a ClassCastException occured!

    Richie Newbie

      There is a java.lang.ClassCastException: ejb.persistence.User.

      My servlet code is:

      private void showUser(HttpServletRequest request,
       HttpServletResponse response) {
       try {
       InitialContext ctx = new InitialContext();
       userSession = (userSession) ctx.lookup("userSessionBean/local");
       } catch (Exception e) {
       e.printStackTrace();
       }
      
       Collection<User> auser = userSession.getUsers();
       if (auser != null)
       for (User user : auser) {
       System.out.println("User name : " + user.getName());
       System.out.println("User id : " + user.getId());
       System.out.println("User mgs : " + user.getMessage());
       }
       }

      userSessionBean/local getUsers method code is:
       public Collection<User> getUsers() {
       // TODO Auto-generated method stub
       return em.createQuery("from User u").getResultList();
       }


      My ejb.persistence.User class is:
      package ejb.persistence;
      
      import javax.persistence.*;
      
      @Entity
      @Table(name = "user")
      public class User {
       private String name;
      
       private int id;
      
       private String message;
      
       public User() { }
      
       public User(String name, String message) {
       this.name = name;
       this.message = message;
       }
      
       @Id
       @GeneratedValue
       public int getId() {
       return id;
       }
      
       public void setId(int id) {
       this.id = id;
       }
      
       public String getMessage() {
       return message;
       }
      
       public void setMessage(String message) {
       this.message = message;
       }
      
       public String getName() {
       return name;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      }


      Sorry for the long post, hope you're still with me. My questions is:
      Why is there a ClassCastException occured? What's wrong?

        • 1. Re: Without rhyme or reason? a ClassCastException occured!
          Richie Newbie

           

          "richie-lea" wrote:
          There is a java.lang.ClassCastException: ejb.persistence.User.

          My servlet code is:
          private void showUser(HttpServletRequest request,
           HttpServletResponse response) {
           try {
           InitialContext ctx = new InitialContext();
           userSession = (userSession) ctx.lookup("userSessionBean/local");
           } catch (Exception e) {
           e.printStackTrace();
           }
          
           Collection<User> auser = userSession.getUsers();
           if (auser != null)
           for (User user : auser) {
           System.out.println("User name : " + user.getName());
           System.out.println("User id : " + user.getId());
           System.out.println("User mgs : " + user.getMessage());
           }
           }

          userSessionBean/local getUsers method code is:
           public Collection<User> getUsers() {
           // TODO Auto-generated method stub
           return em.createQuery("from User u").getResultList();
           }


          My ejb.persistence.User class is:
          package ejb.persistence;
          
          import javax.persistence.*;
          
          @Entity
          @Table(name = "user")
          public class User {
           private String name;
          
           private int id;
          
           private String message;
          
           public User() { }
          
           public User(String name, String message) {
           this.name = name;
           this.message = message;
           }
          
           @Id
           @GeneratedValue
           public int getId() {
           return id;
           }
          
           public void setId(int id) {
           this.id = id;
           }
          
           public String getMessage() {
           return message;
           }
          
           public void setMessage(String message) {
           this.message = message;
           }
          
           public String getName() {
           return name;
           }
          
           public void setName(String name) {
           this.name = name;
           }
          }


          Log msg:
          06:43:44,937 ERROR [[ejb_servlet]] Servlet.service() for servlet ejb_servlet threw exception
          java.lang.ClassCastException: ejb.persistence.User
           at servlet.ejb_servlet.showUser(ejb_servlet.java:51)
           at servlet.ejb_servlet.doGet(ejb_servlet.java:87)
           at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
           .....
          

          Sorry for the long post, hope you're still with me. My questions is:
          Why did the ClassCastException occured? What's wrong?


          • 2. Re: Without rhyme or reason? a ClassCastException occured!
            Richie Newbie

            I changed the servlet :

            private void showUser(HttpServletRequest request,
             HttpServletResponse response) {
             try {
             InitialContext ctx = new InitialContext();
             userSession = (userSession) ctx.lookup("userSessionBean/local");
             } catch (Exception e) {
             e.printStackTrace();
             }
            
             Collection<User> auser = userSession.getUsers();
             System.out.println(auser);
            
             if (auser != null)
             for (User user : auser) {
             System.out.println("User name : " + user.getName());
             System.out.println("User id : " + user.getId());
             System.out.println("User mgs : " + user.getMessage());
             }
             }
            

            Error msg:
            07:09:51,546 INFO [STDOUT] [ejb.persistence.User@200470, ejb.persistence.User@3813c, ejb.persistence.User@19b32e, ejb.persistence.User@1429c57, ejb.persistence.User@264dd8, ejb.persistence.User@c0c6f6, ejb.persistence.User@46752d, ejb.persistence.User@1b71c12]
            07:09:51,546 ERROR [[ejb_servlet]] Servlet.service() for servlet ejb_servlet threw exception
            java.lang.ClassCastException: ejb.persistence.User
             at servlet.ejb_servlet.showUser(ejb_servlet.java:53)
             at servlet.ejb_servlet.doGet(ejb_servlet.java:89)
             at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
            


            • 3. Re: Without rhyme or reason? a ClassCastException occured!
              Ignacio de Cordoba Novice

              Hi there,
              I am having this same problem. Did you find the solution? thanks for any info.
              (If i use remote interface it works OK, but when changing to local lookups of the Stateless sessiion bean, it throws the class cast exception just when invoking the method)

              Thanks,
              Ignacio